NodeJs app with ESM enabled cannot import from shared library
Current Behavior
After converting a Node app to esm, importing from shared library no longer works.
Expected Behavior
Importing from shared library should work the same as with cjs
GitHub Repo
https://github.com/namnguyen191/nx-node-esm-bug
Steps to Reproduce
- Run
npx create-nx-workspace@latestto create a new NX repo with a NodeJs app using theIntegrated monorepooption. - Remove all tests files like e2e or jest
- Convert everything into using esm:
- Add
"type": "module"topackage.json - In
tsconfig.base.json, change:"target": "ESNext","module": "NodeNext",,"moduleResolution": "NodeNext" - In
project.json, change:"format": ["esm"]
- Generate a shared node library called
utils:npx nx generate @nx/node:library --name=utils --directory=libs/utils --projectNameAndRootFormat=as-provided --unitTestRunner=none --no-interactive - Try to import and use the library in the Node app by editing
main.ts
import { utils } from '@node-esm-bug/utils';
console.log(utils());
Nx Report
Node : 20.11.1
OS : darwin-arm64
npm : 10.2.4
nx : 18.0.6
@nx/js : 18.0.6
@nx/jest : 18.0.6
@nx/linter : 18.0.6
@nx/eslint : 18.0.6
@nx/workspace : 18.0.6
@nx/devkit : 18.0.6
@nx/esbuild : 18.0.6
@nx/eslint-plugin : 18.0.6
@nx/node : 18.0.6
@nrwl/tao : 18.0.6
typescript : 5.3.3
Failure Logs
[ watch ] build succeeded, watching for changes...
Debugger listening on ws://localhost:9229/fceb06b6-cba2-4ee8-9e80-377fa21d68f6
Debugger listening on ws://localhost:9229/fceb06b6-cba2-4ee8-9e80-377fa21d68f6
For help, see: https://nodejs.org/en/docs/inspector
Error: Cannot find package '@node-esm-bug/utils' imported from /Users/username/Desktop/projects/node-esm-bug/dist/apps/node-esm-bug/main.js
at packageResolve (node:internal/modules/esm/resolve:853:9)
at moduleResolve (node:internal/modules/esm/resolve:910:20)
at defaultResolve (node:internal/modules/esm/resolve:1130:11)
at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)
at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)
at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
at link (node:internal/modules/esm/module_job:84:36)
Package Manager Version
10.2.4
Operating System
- [X] macOS
- [ ] Linux
- [ ] Windows
- [ ] Other (Please specify)
Additional Information
Setting bundle to true in project.json seems to solve the issue. It seems like lib is no longer being generated as part of the build after converting to esm
I tried the same with Nx 19.0.4 (Linux) and get the same error
Running into the same problem in my project. Any suggestions from the Nx team?
This issue has been automatically marked as stale because it hasn't had any activity for 6 months. Many things may have changed within this time. The issue may have already been fixed or it may not be relevant anymore. If at this point, this is still an issue, please respond with updated information. It will be closed in 21 days if no further activity occurs. Thanks for being a part of the Nx community! 🙏
This is still happening on the latest version
Same here
same issue.
Also having this issue. Particuarly a problem because I can't seem to get a React Router (v7) app to work without ESM ...