Unable to execute node ES Modules
See original GitHub issueCurrent Behavior
When attempting to execute an ES Module with the @nrwl/node:node executor, we get the error reported in the below Failure Logs section. After debugging, I’ve identified that the issue is because the executor is attempting to require the module, which is of course not supported for ES Modules.
Expected Behavior
The ES Module node.js application should properly execute without errors.
Steps to Reproduce
Pull this repo in order to reproduce.
- Create a new node.js project
nx generate @nrwl/node:app - Update
tsconfig.app.jsonwith the following fields:
"module": "esnext",
"target": "es2020"
- Update
project.jsonbuildtarget to use executor@nrwl/node:webpack - Update
project.jsonbuildtarget with the following fields
"outputFileName": "main.mjs",
"webpackConfig": "webpack.config.js"
- Update
project.jsonservetarget to use executor@nrwl/node:node - Create root
webpack.config.jsfile with the following content
module.exports = (config, context) => ({
...config,
experiments: {
...config.experiments,
outputModule: true,
topLevelAwait: true,
},
output: {
path: config.output.path,
chunkFormat: 'module',
library: {
type: 'module',
},
},
})
Failure Logs
/app/node_modules/@nrwl/node/src/executors/node/node-with-require-overrides.js:16
return originalLoader.apply(this, arguments);
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /app/dist/packages/test/main.mjs not supported.
Instead change the require of /app/dist/packages/test/main.mjs to a dynamic import() which is available in all CommonJS modules.
at Function.Module._load (/app/node_modules/@nrwl/node/src/executors/node/node-with-require-overrides.js:16:31)
at Object.<anonymous> (/app/node_modules/@nrwl/node/src/executors/node/node-with-require-overrides.js:19:1)
Environment
$ nx report
> NX Report complete - copy this into the issue template
Node : 16.13.0
OS : darwin x64
npm : 8.1.0
nx : 14.1.7
@nrwl/angular : Not Found
@nrwl/cypress : Not Found
@nrwl/detox : Not Found
@nrwl/devkit : 14.1.7
@nrwl/eslint-plugin-nx : 14.1.7
@nrwl/express : Not Found
@nrwl/jest : 14.1.7
@nrwl/js : 14.1.7
@nrwl/linter : 14.1.7
@nrwl/nest : 14.1.7
@nrwl/next : Not Found
@nrwl/node : 14.1.7
@nrwl/nx-cloud : 14.0.3
@nrwl/nx-plugin : Not Found
@nrwl/react : Not Found
@nrwl/react-native : Not Found
@nrwl/schematics : Not Found
@nrwl/storybook : Not Found
@nrwl/web : Not Found
@nrwl/workspace : 14.1.7
typescript : 4.3.5
rxjs : 7.2.0
---------------------------------------
Community plugins:
Issue Analytics
- State:
- Created a year ago
- Reactions:9
- Comments:11
Top Results From Across the Web
Unable to start Node.js application that uses ES module
It uses ES modules, so I have "type": "module" in package.json . Everything works fine whenever I use npm scripts. Now I'm trying...
Read more >Using ES modules in Node.js - LogRocket Blog
Learn about the state of ES modules in Node today, including concerns realted to transitioning from and interoperability with CommonJS.
Read more >Node Modules at War: Why CommonJS and ES ... - Code Red
All of the “sibling” scripts in the ES module graph download in parallel, but they execute in order, guaranteed by the loader specification....
Read more >node-fetch Error [ERR_REQUIRE_ESM]: require() of ES ...
To solve the node-fetch error [ERR_REQUIRE_ESM]: require() of ES Module not supported, use a dynamic import to import the `node-fetch` package or downgrade ......
Read more >JavaScript modules - MDN Web Docs
Node.js has had this ability for a long time, and there are a number ... of your module, you need to import them...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

My whole project is halted because of this issue, and I’ve been seriously thinking of moving from NX to something else! Only the migration pain is what’s holding me from pulling the trigger now. I’ll probably just do it next couple of days, and move on!
@bulldog98 I tried changing
\node_modules\@nrwl\js\src\executors\node\node-with-require-overrides.jsto usedynamicImportas in #10414Sadly, I’m still getting the same error! I’m not sure if I’m doing something wrong!