[Premium] Nodejs Typescript: cannot import with absolute path in Azure Function
I have this Nodejs Typescript Azure function in local:
<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
This is the 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...\""
},
...
This is the index.ts, which imports a function from ./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> {
...
and this is the ./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....");
And this is 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 OS type;
os: os.type(),
// The machine's OS release;
release: os.release(),
// The machine's OS platform;
platform: os.platform(),
// The machine's OS architecture;
arch: os.arch(),
// The machine's OS uptime;
uptime: os.uptime(),
// The machine's total memory in MegaBytes;
totalMemOfContainerInMB: os.totalmem() / 1024 / 1024,
// the machine's number of CPUs;
numCPUs: os.cpus().length,
};
return machineInfo;
};
Now it is complaining that the in line:
import { getMachineInfo } from "utils/machineInfo";
it cannot find module 'utils/machineInfo':
Error message:
[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
I have tried different things with "baseUrl" and "paths" in tsconfig.json, and the values should be straight-forward and self explanatory now. But apparently I have made a mistake somewhere....
Any suggestions?
To Reproduce
I have made a public minimal reproducible repo at: https://github.com/DaCao/minimal_reproducible_example
To reproduce, git clone this repo and open VS Code:
-
You need to have Azure tools plugin installed in VS Code;
-
In local WORKSPACE, click the azure functions icon and choose:
-
Go through the creation process; select Programming Model V3;
-
After setting up the local project, build the 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
> [email protected] build /Users/john/myapp/code/minimal_reproducible_example
> tsc
john@MacBook-Pro:~/myapp/code/minimal_reproducible_example$
you also need to have Azure Function Core Tools installed:
john@MacBook-Pro:~/myapp/code/minimal_reproducible_example$ func -v
4.0.5198
and then just run:
john@MacBook-Pro:~/myapp/code/minimal_reproducible_example$ func start
you should see the error:
...
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'
...
Hi, I'm facing the same problem. Have you found a workaround for this issue yet?
Same issue here as well, but trying to get things working using the new v4 model
Any luck here @timtucker-dte
I ran into the same problem here. I actually ran into absolute import issue first, then missing file extension that couldn't resolve to files. The funny thing is most advice around the web is to manually add .js extensions to all imports, but this causes issues with unit tests that can't find those files.
You need to transform those absolute paths to relative ones that include a .js extensions to be able to resolve modules. The solution is to install tsc-alias and then configure it.
tsconfig.json
{
"tsc-alias": {
"resolveFullPaths": true,
"verbose": false
}
}
