How to run JS code in Node that imports modules transpiled with other module/moduleResolution?

2 weeks ago 7
ARTICLE AD BOX

I'm using a monorepo with npm workspaces and ZenStack ORM. Code is in TypeScript everywhere. I have a data seeding app that is intended to be run at zenstack seed phase.

All my apps and packages build correctly with `tsc. However I struggle at runtime when running the seed command. It fails when trying to perform a local import in one of my workspace packages.

The structure is as follows:

├── apps │ ├── [...] │ ├── api (node expressjs server) │ ├── web (vite/vue bundle) │ ├── data-seeding ├── etc └── packages ├── [...] ├── lib └── orm

The tsconfig.json of the packages looks like:

"extends": "../../tsconfig.base.json", "compilerOptions": { "module": "esnext", "moduleResolution": "bundler", "declaration": true, "outDir": "dist", "esModuleInterop": true "paths": { "@/*": ["./src/*"] } }, "include": ["src"]

and their package.json:

"type": "module", "exports": { ".": "./dist/index.js" }, "types": "dist/index.d.ts",

I can build my data-seeding app with:

"module": "esnext", "moduleResolution": "bundler",

but not with:

"module": "nodenext", "moduleResolution": "nodenext",

So, when running the main seed script it fails. I tried different ways, with Node or tsx, but I'm not sure why it fails and therefore how to fix it properly.

With tsx:

tsx src/main.ts Cannot find module '@/zenstack/schema' Require stack: - [...]/packages/orm/dist/dbContext.js - [...]/packages/orm/dist/index.js - [...]/apps/data-seeding/src/main.ts

Here @/zenstack/schema is in my orm package.

With node:

node dist/main.js Cannot find module '[...]/apps/data-seeding/dist/seedLogging' imported from [...]/apps/data-seeding/dist/main.js

Here seedLogging is in my data-seeding app.

I presume, without being sure of it, that the problem resides in how Node or tsx have no easy way to solve the modules' paths as they were transpiled with different module/moduleResolution.

Read Entire Article