I tried to write an Azure Functions in a monorepo project written in Typescript, following the Azure Functions guide.
However, when running $ pnpm start
to run the azure functions locally, it showed error about the import path:
Error [ERR_MODULE_NOT_FOUND]: Worker was unable to load entry point "dist/src/functions/example.js":
Cannot find module '/workspace/depot/packages/prisma/node_modules/data/entity'
imported from /workspace/depot/packages/prisma/task.js
I am sure that there is something wrong with the module resolution of these projects, but no idea where the issue is and how to fix it. Because of the requirements of azure function tools (or may not), the function file has to be in ES module, and this requires the .js
extension of the import path for all its dependencies? But the import paths in the generated JS codes in prisma
and data
projects are still in Typescript style. Any idea how to fix it?
Btw, any idea how to make the import path in apps/worker/src/functions/example.ts
cleaner? I think it would be much better to keep 'prisma/task'
rather than 'prisma/dist/task.js'
.
More details below. Let me know if anything else is needed to solve this issue.
Here is the project structure:
.
├── apps
│ └── worker
| ├── src/functions
| | └── example.ts
| ├── package.json
| └── tsconfig.json
├── packages
│ ├── prisma
| | ├── client
| | | ├── index.js
| | | ...
| | |
| | ├── prisma.schema
| | ├── task.ts
| | ├── package.json
| | └── tsconfig.json
│ └── data
| ├── entity.ts
| ├── package.json
| └── tsconfig.json
└── tsconfig.json
The project packages/prisma
imports project packages/data
, and the apps/worker
imports packages/prisma
to connect to the database:
/// apps/worker/src/functions/example.ts
// This module won't be found.
import { getTask } from 'prisma/task';
// This works.
import { getTask } from 'prisma/dist/task.js';
/// packages/prisma/task.ts
// I need to keep this because it is also used by other projects.
import { Entity } from 'data/entity';
Generated packages/prisma/task.ts
js code:
/// packages/prisma/dist/task.js
// Module import path is not changed.
import { Entity } from 'data/entity';
Here is the package.json
file for worker project:
/// apps/worker/package.json
{
...
"main": "dist/src/functions/*.js",
"type": "module", // required by azure function tools?
"scripts": {
"build": "tsc -b",
"prestart": "pnpm run clean && pnpm run build",
"start": "func start"
},
"dependencies": {
"prisma": "workspace:*",
"data": "workspace:*",
...
},
"devDependencies": {
"azure-functions-core-tools": "^4.x",
...
}
}
Here are the tsconfig.ts
files:
/// tsconfig.json
{
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"target": "es2021",
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"outDir": "dist",
"rootDir": ".",
"jsx": "preserve",
"incremental": true
}
}
/// apps/worker/tsconfig.json
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"sourceMap": true
},
"references": [
{ "path": "../../packages/data" },
{ "path": "../../packages/prisma" }
]
}
/// packages/prisma/tsconfig.json
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"noEmit": false,
"composite": true
},
"include": ["**/*.ts", "**/*.d.ts", "**/*.js"],
"exclude": ["node_modules", "client/**/*", "dist"],
"references": [{ "path": "../data" }]
}
/// packages/data/tsconfig.json
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"noEmit": false,
"composite": true
},
"include": ["**/*.ts"],
"exclude": ["node_modules", "dist"]
}
For the packages/prisma/client
, it is generated by the prisma from the prisma.schema
file, and it should not be re-compiled by tsc
and we can just copy/paste it to the dist/
directory I guess?
Palt Mp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.