nx icon indicating copy to clipboard operation
nx copied to clipboard

NodeJs app with ESM enabled cannot import from shared library

Open namnguyen191 opened this issue 2 years ago • 1 comments

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

  1. Run npx create-nx-workspace@latest to create a new NX repo with a NodeJs app using the Integrated monorepo option.
  2. Remove all tests files like e2e or jest
  3. Convert everything into using esm:
  • Add "type": "module" to package.json
  • In tsconfig.base.json, change: "target": "ESNext", "module": "NodeNext",, "moduleResolution": "NodeNext"
  • In project.json, change: "format": ["esm"]
  1. 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
  2. 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

namnguyen191 avatar Mar 01 '24 16:03 namnguyen191

I tried the same with Nx 19.0.4 (Linux) and get the same error

colorcube avatar May 18 '24 16:05 colorcube

Running into the same problem in my project. Any suggestions from the Nx team?

hendrickson-tyler avatar May 26 '24 21:05 hendrickson-tyler

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! 🙏

github-actions[bot] avatar Nov 23 '24 00:11 github-actions[bot]

This is still happening on the latest version

itizarsa avatar Nov 23 '24 03:11 itizarsa

Same here

anthonyfabrizi avatar Dec 21 '24 14:12 anthonyfabrizi

same issue.

wangtuyao avatar Jan 15 '25 05:01 wangtuyao

Also having this issue. Particuarly a problem because I can't seem to get a React Router (v7) app to work without ESM ...

craigsmitham avatar Jan 27 '25 02:01 craigsmitham