Skip to content
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 ES Modules #10565

Closed
jmattiace opened this issue Jun 3, 2022 · 15 comments · Fixed by #10414
Closed

Unable to execute node ES Modules #10565

jmattiace opened this issue Jun 3, 2022 · 15 comments · Fixed by #10414
Labels
outdated scope: node Issues related to Node, Express, NestJS support for Nx type: bug

Comments

@jmattiace
Copy link

jmattiace commented Jun 3, 2022

Current 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.

  1. Create a new node.js project nx generate @nrwl/node:app
  2. Update tsconfig.app.json with the following fields:
"module": "esnext",
"target": "es2020"
  1. Update project.json build target to use executor @nrwl/node:webpack
  2. Update project.json build target with the following fields
"outputFileName": "main.mjs",
"webpackConfig": "webpack.config.js"
  1. Update project.json serve target to use executor @nrwl/node:node
  2. Create root webpack.config.js file 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:

@AgentEnder AgentEnder added the scope: node Issues related to Node, Express, NestJS support for Nx label Jun 3, 2022
@bulldog98
Copy link
Contributor

This is fixed by #10414

@jmattiace
Copy link
Author

I just confirmed that #10414 fixes the issue. Thanks @bulldog98! Do you have any idea when this will get merged in and released?

@bulldog98
Copy link
Contributor

@jmattiace no plan when it will be merged, I'm still waiting for a maintainer to run the test, after that it should be merged

@KeithGillette
Copy link

I am getting the same error trying to migrate a project that depends on lodash-es to use the @nrwl/node:node executor in Nx 14.3.2. I have made the changes in step 1-6 in the Steps to Reproduce and modified node-with-require-overrides.js in my local node_modules with the changes in this PR but still get the same error. Any troubleshooting advice?

@jmattiace
Copy link
Author

@KeithGillette the changes in #10414 fixed the issue for me. I would suggest using a debugger to ensure that the changes you made in the dependency file have in fact taken effect.

@KeithGillette
Copy link

KeithGillette commented Jun 22, 2022

Thank, @jmattiace. It turns out the custom webpack.config.js was not being run because in step 4 the noted fields actually need to go into the build.options property (duh!). That change executed the modified node-with-require-overrides.js but still left me with a similar but different error:

(node:52911) UnhandledPromiseRejectionWarning: ReferenceError: require is not defined in ES module scope, you can use import instead

@omsharp
Copy link

omsharp commented Oct 22, 2022

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
Copy link
Contributor

@omsharp you could patch the @nrwl/node module (it was moved in a later version of nx) where you just replace in src/executors/node/node-with-require-overrides.js the require with an import, that is a hotfix, until the nx team decides to introduce proper esm support

@omsharp
Copy link

omsharp commented Oct 23, 2022

@bulldog98
I tried changing \node_modules\@nrwl\js\src\executors\node\node-with-require-overrides.js to use dynamicImport as in #10414

Sadly, I'm still getting the same error! I'm not sure if I'm doing something wrong!

@bulldog98
Copy link
Contributor

oh you just have to write instead of require import in the js file, the problem why you can not do that in typescript, is because it import gets replaced by require with the settings of typescript nx uses.

@Tilesto
Copy link

Tilesto commented Dec 25, 2022

The same error for the bundle: Nest + Angular 15 + NG Universal.

Fully agree with it((

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!

@sovcik
Copy link

sovcik commented Mar 6, 2023

Well, the same issue here, but started appearing after I added package get-folder-size to the project. Removing it "fixed" the issue. Maybe you could use that package to reproduce the error.
Using NX 15.7.2, node 19.6

@B-R-Bender
Copy link

B-R-Bender commented Mar 17, 2023

same here with https://github.com/meetecho/janode
dynamicImport doesn't help
any help, please?

@arielweinberger
Copy link

Still an issue.

@github-actions
Copy link

github-actions bot commented Jun 4, 2023

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: node Issues related to Node, Express, NestJS support for Nx type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants