-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to execute node when importing ES Modules - using @nx/js:node executor #21928
Comments
@richmondp have you found a solution to this? Running into the same issue |
+1, occurring in v19 also |
See also #11335 and my comment #11335 (comment) |
In case it is useful, I've published a Node executor including ESM module resolution for buildable libraries within Nx workspaces. |
+1. When will @nx/js:node be ESM compatible? |
Also just ran headlong into this. Using nx v19.0.3, developing a Node.js application using I switched the
I searched around for and found some issues on the esbuild repo like this one about what to do evanw/esbuild#1343 — the TS team says you should manually add I went ahead and did that for my relative imports. But then still ran into an error where I'm importing libraries from elsewhere in my monorepo and those can't be found. What I ended up going with was specifying Would definitely appreciate an easier, clearer path here. |
Is there any workaround? This problem has been around for about 2 years now. |
For me it works by just changing the target and module in tsconfig.json to ES2022 and changing the @nx/esbuild:esbuild executor options to "format": ["esm"], |
@gionkunz Could you share a working repo? I have the same options, but it's not working |
+1 |
Not a solution per se but node 22 lts now supports importing esm modules via require, if you can update your node version to 22.12 and above this is solved for you. |
Current Behavior
When attempting to execute a node/express application using @nx/js:node executor, if the application imports an ES Module library with the @nrwl/node:node executor the following error is displayed.
If, I then make some changes to the application in an attempt to support importing ESM modules, I am then faced with another issue in that I cannot import any workspace libraries.
ERROR in ./apps/webpack-mod/src/main.ts 12:0-46 Module not found: Error: Can't resolve '@esm-test/cjs-lib' in '.../esm-test/apps/webpack-mod/src'
Expected Behavior
Should be able to execute a project that imports an ES Module library and workspace libraries which themselves, may or may not import other 3rd party ES Module libraries.
And/or potentially, in the interim, some documentation describing how this can best be achieved in current versions. From reading other threads, this seems to be a common issue raised and with ESM libraries being the recommendation moving forward more and more libraries will move this way. I am assuming there is a way to deal with this, but as yet, I have been unable to find a correct approach (see below for details of each approach). If it is not something that can be easily added to the library then I am open to writing my own executors/generators but I am a little lost as to how to proceed.
I know the examples in the attached repo are very basic and in some cases I could just downgrade a version of a library I need but I have some libraries that this is not possible.
As it stands right now, I am unable to continue using nx because of the issues in executing apps with ESM library dependencies but I don't want to do that because I like the NX ecosystem.
GitHub Repo
https://github.com/b-simpson12/esm-test
Steps to Reproduce
I have attached an git repo of an nx workspace containing a number of different node/express applications. In each app I am trying to import a 3rd party ESM library (in this example lodash-es) as well a 3 workspace JS libraries.
format: ["esm"]
in the build config as well as modifications topackage.json
andtsconfig.lib.json
to set it up to be an esm library.Note: All app have a README outlining how it was created and what changes were made after the out-of-the-box generated code
Applications
dynamic-import: Express application using Dynamic Imports for esm libraries
nx run dynamic-import:build
- (Works)nx run dynamic-import:serve
- (Fails with Error [ERR_REQUIRE_ESM]: require() of ES Module)esbuild-mod: Express application which has been modified to use @nx/esbuild:esbuild as its build executor
nx run esbuild-mod:build
- (Fails - does not recognize libs) - WORKS with only esm module importnx run esbuild-mod:serve
- (Fails - does not recognize libs) - WORKS with only esm module importissue-10565: Node application - based of the directions in Issue 10565
I wanted to see what the outcome of PR 10414 was which seem to indicate that it would fix this issue.
nx run issue-10565:build
- (Fails - does not recognize libs) - WORKS with only esm module importsnx run issue-10565:serve
- (Fails - does not recognize libs) - WORKS with only esm module importsissue-10565: Node application - based of the directions in Issue 10565
Basically the same as issue-105655 but just using the express generator
nx run express-10565:build
- (Fails - does not recognize libs) - WORKS with only esm module importsnx run express-10565:serve
- (Fails - does not recognize libs) - WORKS with only esm module importsnode-app: Out-of-the-box Node application
Used to compare the differences between the standard generated Node app and the modifications made in issue-10565
nx run node-app:build
- Worksnx run node-app:serve
- Fails - Error [ERR_REQUIRE_ESM]: require()preset-express: Express application created with the workspace using --preset=express
nx run preset-express:build
- Worksnx run preset-express:serve
- Fails - Error [ERR_REQUIRE_ESM]: require() on esm library import or workspace libraries that have esm importswebpack-mod: Express applications with modifications to the webpack executor (among other things)
This was based off information in a number of different threads and was pieced together to form this app
nx run webpack-mod:build
- (Fails - does not recognize libs) - WORKS with only esm module importnx run webpack-mod:serve
- (Fails - does not recognize libs) - WORKS with only esm module importcontrol: An express app with no additional imports
Used as a baseline
nx run control:build
- Worksnx run control:serve
- WorksNx Report
Failure Logs
Package Manager Version
No response
Operating System
Additional Information
No response
The text was updated successfully, but these errors were encountered: