gpt4 book ai didi

node.js - 任何人都可以引导我朝着正确的方向让这个 Azure Functions 输出 "Cannot Find Module ' @azure/functions' 吗?

转载 作者:行者123 更新时间:2023-12-03 00:22:53 26 4
gpt4 key购买 nike

我正在尝试使用 TypeScript 在本地运行 Azure Function。我在 TypeScript 和 JS 领域有相当多的经验,但在这些消费类型的云服务方面却没有那么多经验。​我正在运行最新版本的核心工具,因此运行 func -v 返回 3.0.3160​我尝试重新安装工具。同一问题在多台计算机和操作系统上仍然存在。​

函数.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;

package.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"
}

tsconfig.json

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

主机.json

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

函数.json


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

错误

​在本地运行 azure 函数时,我运行“yarn run build:start”并从它执行的脚本元素中运行 yarn 清洁、 yarn 构建和 yarn 运行开始。终端是这样的输出到控制台。​

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_modules 的不稳定问题(出于多种原因),因此值得删除该文件夹并再次运行 yarn

您没有发布 yarn.lock 文件来验证 @azure/functions 是否已实际安装;忽视似乎是一件愚蠢的事情,但确实有可能发生。

看起来您还缺少 function.ts 的导入。

import { AzureFunction, Context, HttpRequest } from "@azure/functions";

https://www.npmjs.com/package/@azure/functions#usage

关于node.js - 任何人都可以引导我朝着正确的方向让这个 Azure Functions 输出 "Cannot Find Module ' @azure/functions' 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65431913/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com