Azure / Azure/Azure-Functions

[Premium] Nodejs Typescript: cannot import with absolute path in Azure Function

Open
#2,397 4 comments 0 reactions 0 assignees View on GitHub
premium-plan
Dominant language
PowerShell
Stars
1.1k
Forks
215
Avg merge
4h 2m
Merged PRs (30d)
1

Description

I have this Nodejs Typescript Azure function in local:
```

├── 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 {

...

```
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:

1. You need to have Azure tools plugin installed in VS Code;

2. In local WORKSPACE, click the azure functions icon and choose:

[![Create Functon...][1]][1]

3. Go through the creation process; select Programming Model V3;

4. 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

> minimal_reproducible_example@1.0.0 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'

...

```

[1]: https://i.stack.imgur.com/fTfic.png

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.