nx
nx copied to clipboard
@nrwl/node:node executor does not support importing ESM libs
Current Behavior
Error [ERR_REQUIRE_ESM]: require() of ES Module *** from *** not supported.
Expected Behavior
Npm libs bundled with ESM only module should be supported.
Steps to Reproduce
- Create a new NX workspace and generate a
@nrwl/node
application. - Add an NPM package that is only bundled with ESM, e.g.
yarn add @keycloak/keycloak-admin-client
- Import this package in
main.ts
- Run
nx serve
- Observe the error message listed in the current behaviour section.
- Run
nx build
- Observe no errors, build is successful.
Failure Logs
Error [ERR_REQUIRE_ESM]: require() of ES Module *** from *** not supported.
Environment
Node : 16.15.0 OS : darwin arm64 yarn : 1.22.19
nx : 14.1.1 @nrwl/angular : Not Found @nrwl/cypress : 14.1.1 @nrwl/detox : Not Found @nrwl/devkit : 14.1.1 @nrwl/eslint-plugin-nx : 14.1.1 @nrwl/express : 14.1.9 @nrwl/jest : 14.1.1 @nrwl/js : 14.1.1 @nrwl/linter : 14.1.1 @nrwl/nest : Not Found @nrwl/next : Not Found @nrwl/node : 14.1.9 @nrwl/nx-cloud : Not Found @nrwl/nx-plugin : Not Found @nrwl/react : 14.1.1 @nrwl/react-native : Not Found @nrwl/schematics : Not Found @nrwl/storybook : 14.1.1 @nrwl/web : 14.1.1 @nrwl/workspace : 14.1.1 typescript : 4.6.4 rxjs : 6.6.7
Possibly related issue: https://github.com/nrwl/nx/issues/10296
+1, i have the same problem
Check out #10565 and #10414. Waiting for good news.
I have the same import KcAdminClient from '@keycloak/keycloak-admin-client'; const kcAdminClient = new KcAdminClient( { baseUrl: process.env.KEYCLOAK_URL, realmName: process.env.REALM_NAME } );
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\.....
same issue: can't import nanoid 4.0
, which has only ESM
support. I'm using @nrwl/nest 14.1.7
Same here. Right now my whole "Nx experience" feels like product owners went all crazy with plugins, "build anything" capabilities, and left behind the simplicity and truly greatness of nx
: its tools for managing development directories.
We are running into this same issue with nanoid 4.0
and other ESM only libraries. This is a major blocker for us!
any updates ? Hitting the same issue
Do we know if this will be fixed any time soon? Any timeline?
Bump
Any updates?
I just had to update one of my dependencies, which is now ESM-only. I have yet to find a way to make it work with nx, and I'm wondering if there is even a viable workaround for this?
I have same issue
I have same issue. I can use dynamic import but that requires await
and so restructure of the code.
Running project with esbuild bundler works but it has issue related to debugger. https://github.com/nrwl/nx/issues/14556
just in case anyone wants to use esbuild
Hello! Any update on this? Still can't find a way to make this work 😓
It is difficult to ask support for this subject without providing a sample repository to work with. you should at least use typescript 4.7
I made a node application and used this import and it worked .
import KcAdminClient from '@keycloak/keycloak-admin-client'; const kcAdminClient = new KcAdminClient( { baseUrl: process.env.KEYCLOAK_URL, realmName: process.env.REALM_NAME } );
my environment : Node : 16.18.0 OS : win32 x64 npm : 9.2.0
nx : 15.6.3 @nrwl/angular : Not Found @nrwl/cypress : 15.6.3 @nrwl/detox : Not Found @nrwl/devkit : 15.6.3 @nrwl/esbuild : 15.6.3 @nrwl/eslint-plugin-nx : 15.6.3 @nrwl/expo : Not Found @nrwl/express : Not Found @nrwl/jest : 15.6.3 @nrwl/js : 15.6.3 @nrwl/linter : 15.6.3 @nrwl/nest : Not Found @nrwl/next : Not Found @nrwl/node : 15.6.3 @nrwl/nx-cloud : Not Found @nrwl/nx-plugin : Not Found @nrwl/react : 15.6.3 @nrwl/react-native : Not Found @nrwl/rollup : Not Found @nrwl/schematics : Not Found @nrwl/storybook : Not Found @nrwl/web : Not Found @nrwl/webpack : 15.6.3 @nrwl/workspace : 15.6.3 @nrwl/vite : 15.6.3 typescript : 4.8.4
Local workspace plugins:
Community plugins:
Same here with nanoid version 4.0.1 :/, any news?
Same here with nanoid version 4.0.1 :/, any news?
@alejandrombc nobody can solve your problem, if you don't provide sample code repository AND environment . As I wrote it works.
That depends on your project setup. something is missing or badly setted up .....
As I write this comment the issue still persists with NX 15.7.2 and TypeScript 4.9.5.
Same for both @keycloak/[email protected]
and [email protected]
.
Also out of curiosity, I tried the new serve executor @nrwl/js:node
/ build executor @nrwl/esbuild:esbuild
and the result is the same.
I'm not sure @alejandrombc why you cannot reproduce the issue because there is nothing really custom setup. You get the error straight away with all default settings.
If I'm getting more feedback of not replicable I will consider spending some time to create a repo. Although I doubt how useful will that be since everything is default NX settings.
same issue with nanoid 4.0.2. I updated the whole dependencies (nx migrate --run-migrations) and still have the error.
@sancelot I made a reproduction repo showing the issue (for me at least) https://github.com/christopher-caldwell/esm-require
Tested @christopher-caldwell against the new 16.0.0-beta.1 and still fails with the same error:
[ watch ] build succeeded, watching for changes...
Debugger listening on ws://localhost:9229/e5b6f3bd-c0db-4a6f-b2b6-766685931683
For help, see: https://nodejs.org/en/docs/inspector
/home/caio/github/christopher-caldwell/esm-require/node_modules/@nx/js/src/executors/node/node-with-require-overrides.js:16
return originalLoader.apply(this, arguments);
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/caio/github/christopher-caldwell/esm-require/dist/packages/api/main.js from /home/caio/github/christopher-caldwell/esm-require/node_modules/@nx/js/src/executors/node/node-with-require-overrides.js not supported.
Instead change the require of main.js in /home/caio/github/christopher-caldwell/esm-require/node_modules/@nx/js/src/executors/node/node-with-require-overrides.js to a dynamic import() which is available in all CommonJS modules.
at Module._load (/home/caio/github/christopher-caldwell/esm-require/node_modules/@nx/js/src/executors/node/node-with-require-overrides.js:16:31)
at Object.<anonymous> (/home/caio/github/christopher-caldwell/esm-require/node_modules/@nx/js/src/executors/node/node-with-require-overrides.js:19:1)
My current solution for an esm module built from esbuild is to update node-with-require-overrides.js
and use patch-package
https://github.com/nrwl/nx/pull/10414#issuecomment-1369314024
// node-with-require-overrides.js line 19
// require(fileToRun);
import(fileToRun).catch((error) => {
console.error('Error importing main module:', error);
});
Here's another repro with the steps taken from the offical docs for nx (without any changes + installing chalk): https://github.com/nicu-chiciuc/repro-nx-chalk
At this point, I'm thinking about switching from Nx to a different stack, since I've been stuck on these issues for several weeks.
Any updates on this issue? Are there any workarounds to just make it work?
We're having the same issue with turkey-neighbourhoods@^3
- https://github.com/muratgozel/turkey-neighbourhoods/issues/9
i have a barely instantiated nestjs app and a node library that exports a planetscale database connection. as soon as anything is imported from the node library i get this....
F:\...\product-suite\node_modules\.pnpm\@[email protected]_@[email protected]_@[email protected][email protected][email protected]\node_modules\@nx\js\src\executors\node\node-with-require-overrides.js:18
return originalLoader.apply(this, arguments);
^
Error [ERR_REQUIRE_ESM]: require() of ES Module F:\...\product-suite\node_modules\.pnpm\@[email protected]\node_modules\@planetscale\database\dist\index.js from F:\...\haven-world-tours\product-suite\dist\apps\web-server\main.js not supported.
Instead change the require of index.js in F:\client-projects\haven-world-tours\product-suite\dist\apps\web-server\main.js to a dynamic import() which is available in all CommonJS modules.
just brought my whole initial startup deployment to a crashing halt...
@sancelot I made a reproduction repo showing the issue (for me at least) https://github.com/christopher-caldwell/esm-require
I was able to get this working by changing the build target options. use "format": ["cjs"]
and "bundle": true
This will produce CommonJS output in the /dist/packages/api
directory
@mattfysh where do you change this ?
@dmastag look for the project.json file in the subfolder, you'll find these options in the build target, e.g.
https://github.com/christopher-caldwell/esm-require/blob/255b3250325ab03616931d15c34115f73f300bfc/packages/api/project.json#L14-L15
Thanks @mattfysh
Sadly this still doesn't work on my project when I tried it 😞
I am using the latest "@nx/node": "16.5.5" So that might be the problem
Somehow, I was able to get it working with the newer executors. https://github.com/christopher-caldwell/esm-require/tree/working