gpt4 book ai didi

Node.js typescript Azure 函数 : Cannot import with absolute path ( minimal reproducible example attached)

转载 作者:行者123 更新时间:2023-12-03 05:10:57 26 4
gpt4 key购买 nike

我在本地有这个 Nodejs Typescript Azure 函数:

<root-directory>
├── README.md
├── dist
├── host.json
├── local.settings.json
├── node_modules
├── package-lock.json
├── package.json
├── sealworker
│ ├── constants
│ ├── errors
│ ├── function.json
│ ├── index.ts
│ ├── interfaces
│ ├── sample.dat
│ ├── services
│ │ ├── worker.ts
│ │ └── ...
│ └── utils
│ ├── machineInfo.ts
│ └── ...
└── tsconfig.json

这是tsconfig.json:

{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"target": "es6",
"outDir": "dist",
"rootDir": ".",
"sourceMap": true,
"strict": false,
"esModuleInterop": true,
"preserveConstEnums": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": "./sealworker",
"paths": {
"*": [
"*",
"sealworker/*",
"node_modules/*"
]
}
},
"exclude": [
"node_modules",
"**/*.test.ts"
],
"include": [
"**/*"
]
}

package.json:

{
"name": "azure_function",
"version": "1.0.0",
"description": "",
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"clean": "rimraf dist",
"prestart": "npm run clean && npm run build",
"start": "func start",
"test": "echo \"No tests yet...\""
},

...

这是index.ts,它从./services/worker.ts导入一个函数:

import { AzureFunction, Context, HttpRequest } from "@azure/functions";
import { workerExec } from "./services/worker";

const sealWorkerFunction: AzureFunction = async function (
context: Context,
req: HttpRequest
): Promise<void> {

...

这是./services/worker.ts:

import "dotenv/config";
import "reflect-metadata";
import { HttpRequest, Context } from "@azure/functions";
import { getMachineInfo } from "utils/machineInfo";

export const workerExec = async (req: HttpRequest, context: Context) => {
context.log("start....");

这是utils/machineInfo.ts:

import os from "os";

// Get the machine info that identifies the current host running this code;
export const getMachineInfo = () => {
const machineInfo = {
// The machine's hostname;
hostname: os.hostname(),
// the machine's number of CPUs;
numCPUs: os.cpus().length,
};
return machineInfo;
};

现在它提示内联:

import { getMachineInfo } from "utils/machineInfo";

找不到模块“utils/machineInfo”:

错误消息:

[2023-08-10T03:59:00.616Z] Worker was unable to load entry point "dist/sealworker/index.js": Cannot find module 'utils/machineInfo'
[2023-08-10T03:59:00.616Z] Require stack:
[2023-08-10T03:59:00.616Z] - /Users/code/seal/azure_function/dist/sealworker/services/worker.js
[2023-08-10T03:59:00.616Z] - /Users/code/seal/azure_function/dist/sealworker/index.js
[2023-08-10T03:59:00.616Z] - /usr/local/Cellar/azure-functions-core-tools@4/4.0.5198/workers/node/dist/src/worker-bundle.js
[2023-08-10T03:59:00.616Z] - /usr/local/Cellar/azure-functions-core-tools@4/4.0.5198/workers/node/dist/src/nodejsWorker.js

我在 tsconfig.json 中使用 "baseUrl""paths" 尝试了不同的操作,并且这些值应该是直接的现在不言自明了。但显然我在某个地方犯了错误......

有什么建议吗?

---------更新---------

我已经在以下位置创建了一个公共(public)的最小可复制存储库:https://github.com/DaCao/minimal_reproducible_example

要重现,请 git 克隆此存储库并打开 VS Code:

  1. 您需要在 VS Code 中安装 Azure 工具插件;

  2. 在本地工作空间中,单击 Azure 函数图标并选择:

Create Functon...

  • 完成创建过程;选择编程模型V3;

  • 设置本地项目后,构建nodejs:

  • john@MacBook-Pro:~/myapp/code/minimal_reproducible_example$ ls
    host.json local.settings.json node_modules/ package-lock.json package.json sealworker/ tsconfig.json
    john@MacBook-Pro:~/myapp/code/minimal_reproducible_example$ npm run-script build

    > <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c11151215111d10230e190c0e1318091f151e10192319041d110c10193c4d524c524c" rel="noreferrer noopener nofollow">[email protected]</a> build /Users/john/myapp/code/minimal_reproducible_example
    > tsc

    john@MacBook-Pro:~/myapp/code/minimal_reproducible_example$

    您还需要安装Azure Function Core Tools:

    john@MacBook-Pro:~/myapp/code/minimal_reproducible_example$ func -v 
    4.0.5198

    然后运行:

    john@MacBook-Pro:~/myapp/code/minimal_reproducible_example$ func start

    您应该看到错误:


    ...

    For detailed output, run func with --verbose flag.
    [2023-08-11T05:38:06.464Z] Worker process started and initialized.
    [2023-08-11T05:38:06.474Z] Worker was unable to load function sealworker: 'Cannot find module 'services/JobProcessor'
    [2023-08-11T05:38:06.474Z] Require stack:
    [2023-08-11T05:38:06.474Z] - /Users/john/myapp/code/minimal_reproducible_example/dist/sealworker/services/worker.js
    [2023-08-11T05:38:06.474Z] - /Users/john/myapp/code/minimal_reproducible_example/dist/sealworker/index.js
    [2023-08-11T05:38:06.474Z] - /usr/local/Cellar/azure-functions-core-tools@4/4.0.5198/workers/node/dist/src/worker-bundle.js
    [2023-08-11T05:38:06.474Z] - /usr/local/Cellar/azure-functions-core-tools@4/4.0.5198/workers/node/dist/src/nodejsWorker.js'

    ...

    最佳答案

    根据您的堆栈跟踪,我猜您使用 tsc 编译代码,然后使用 node 执行。请记住,如果您使用 tsc 构建,绝对路径最终将根本不会转换为相对路径。

    因此,我建议采取额外的步骤,使用 tsc-alias 来转换这些路径。

    // package.json
    {
    "build": "tsc && tsc-alias",
    }

    查看示例存储库后更新:

    我注意到您设置了 rootDir: "." ,它将在同一目录 sealworker 下包含您构建的代码,但不幸的是,此设置会阻止 tsc-alias 由于某种我也不知道的原因而无法正常工作(向他们报告可能是一个错误)。

    无论如何,我建议设置另一个命令将构建的代码移动到目录中,甚至删除此设置并将构建的代码放在 dist 下就足够了。

    关于Node.js typescript Azure 函数 : Cannot import with absolute path ( minimal reproducible example attached),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76872587/

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