首页 > 解决方案 > 任何人都可以引导我朝着正确的方向前进,让这个 Azure 函数在它输出“无法找到模块'@azure/functions'的地方?

问题描述

我正在尝试使用 TypeScript 在本地运行 Azure 函数。我在 TypeScript 方面有相当多的经验,而且通常在 JS 世界中,但在这些消费型云服务方面却没有那么多经验。我正在运行最新版本的核心工具,所以运行func -v返回3.0.3160 我尝试重新安装工具。同样的问题在多台机器和操作系统中仍然存在。​</p>

函数.ts

const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
    context.log('HTTP trigger function processed a request.');
    
    // Contents of foo are irrelevant
    foo(context, req);
    context.res = {
        status: 200,
    };
};
export default httpTrigger;

包.json

"scripts": {
    "clean": "rm -rf ./dist/*",
    "build": "yarn run clean && tsc",
    "prestart": "yarn run build && func extensions install",
    "start:host": "func start --verbose",
    "start": "yarn run start:host && yarn run watch",
    "build:start": "yarn run build && yarn run start:host",
},
"dependencies": {
    "@types/node": "^12.12.6",
    "applicationinsights": "^1.8.8",
    "axios": "^0.21.0",
    "case": "^1.6.3",
    "node-fetch": "^2.6.1",
    "tslib": "^2.0.3",
    "@azure/functions": "^1.2.2"
},
"devDependencies": {
    "@types/node-fetch": "^2.5.7",
    "typescript": "^4.1.2"
}

​</p>

tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "outDir": "dist",
        "rootDir": "./src",
        "moduleResolution": "Node"
    },
    "exclude": [
        "node_modules"
    ]
}

​</p>

主机.json

​</p>

{
  "version": "2.0",
  "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[1.*, 2.0.0]"
    },
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  }
}

​</p>

函数.json

​</p>

​
{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ],
  "scriptFile": "../dist/function.js"
}

​</p>

错误

​ 在本地运行 azure 函数时,我运行“yarn run build:start”,并从脚本元素执行 yarn clean、yarn build 和 yarn run start。这是终端输出到控制台的内容。​</p>

Worker was unable to load function src: 'Error: Cannot find module '@azure/functions'
[2020-12-23T19:16:24.136Z] Require stack:
[2020-12-23T19:16:24.136Z] - /~/Projects/events/dist/function.js
[2020-12-23T19:16:24.136Z] - /usr/local/Cellar/azure-functions-core-tools@2/2.7.3023/workers/node/worker-bundle.js
[2020-12-23T19:16:24.136Z] - /usr/local/Cellar/azure-functions-core-tools@2/2.7.3023/workers/node/dist/src/nodejsWorker.js'
[2020-12-23T19:16:24.146Z] Worker failed to function id cef7538d-d82e-400d-b29a-dbb3dacf3e7f.
[2020-12-23T19:16:24.146Z] Result: Failure
[2020-12-23T19:16:24.147Z] Exception: Worker was unable to load function src: 'Error: Cannot find module '@azure/functions'
[2020-12-23T19:16:24.147Z] Require stack:
[2020-12-23T19:16:24.147Z] - /~/Projects/events/dist/function.js
[2020-12-23T19:16:24.147Z] - /usr/local/Cellar/azure-functions-core-tools@2/2.7.3023/workers/node/worker-bundle.js
[2020-12-23T19:16:24.147Z] - /usr/local/Cellar/azure-functions-core-tools@2/2.7.3023/workers/node/dist/src/nodejsWorker.js'
[2020-12-23T19:16:24.147Z] Stack: Error: Cannot find module '@azure/functions'
[2020-12-23T19:16:24.147Z] Require stack:
[2020-12-23T19:16:24.147Z] - /~/Projects/events/dist/function.js
[2020-12-23T19:16:24.147Z] - /usr/local/Cellar/azure-functions-core-tools@2/2.7.3023/workers/node/worker-bundle.js
[2020-12-23T19:16:24.147Z] - /usr/local/Cellar/azure-functions-core-tools@2/2.7.3023/workers/node/dist/src/nodejsWorker.js
[2020-12-23T19:16:24.147Z]     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
[2020-12-23T19:16:24.147Z]     at Function.Module._load (internal/modules/cjs/loader.js:667:27)
[2020-12-23T19:16:24.147Z]     at Module.require (internal/modules/cjs/loader.js:887:19)
[2020-12-23T19:16:24.147Z]     at Module.patchedRequire [as require] (/~/Projects/events/node_modules/diagnostic-channel/dist/src/patchRequire.js:14:46)
[2020-12-23T19:16:24.147Z]     at require (internal/modules/cjs/helpers.js:74:18)
[2020-12-23T19:16:24.147Z]     at Object.<anonymous> (/~/Projects/events/dist/function.js:47:1)
[2020-12-23T19:16:24.147Z]     at Module._compile (internal/modules/cjs/loader.js:999:30)
[2020-12-23T19:16:24.147Z]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
[2020-12-23T19:16:24.147Z]     at Module.load (internal/modules/cjs/loader.js:863:32)
[2020-12-23T19:16:24.147Z]     at Function.Module._load (internal/modules/cjs/loader.js:708:14).

标签: node.jstypescriptazureazure-functions

解决方案


'错误:找不到模块'@azure/functions'

从您显示的错误来看,错误似乎来自没有天蓝色功能模块的 node_modules 文件夹。因此,您可以在终端中执行全局 install-module 命令:

npm install @azure/functions

之后,应该没问题(请与管理员一起启动VS Code。)。


推荐阅读