nx icon indicating copy to clipboard operation
nx copied to clipboard

@nrwl/node:node executor does not support importing ESM libs

Open owen26 opened this issue 2 years ago • 55 comments

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

owen26 avatar Jul 28 '22 14:07 owen26

+1, i have the same problem

klerick avatar Aug 02 '22 09:08 klerick

Check out #10565 and #10414. Waiting for good news.

stomvi avatar Aug 02 '22 20:08 stomvi

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:\.....

edsonprecoder avatar Aug 04 '22 19:08 edsonprecoder

same issue: can't import nanoid 4.0, which has only ESM support. I'm using @nrwl/nest 14.1.7

davidgabrichidze avatar Aug 10 '22 17:08 davidgabrichidze

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.

moatorres avatar Oct 11 '22 14:10 moatorres

We are running into this same issue with nanoid 4.0 and other ESM only libraries. This is a major blocker for us!

bilalshaikh42 avatar Nov 17 '22 02:11 bilalshaikh42

any updates ? Hitting the same issue

jmls avatar Dec 01 '22 21:12 jmls

Do we know if this will be fixed any time soon? Any timeline?

nicolabello avatar Dec 08 '22 20:12 nicolabello

Bump

BugGambit avatar Dec 13 '22 00:12 BugGambit

Any updates?

Mrhoho avatar Dec 16 '22 07:12 Mrhoho

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?

quaelin avatar Jan 02 '23 20:01 quaelin

I have same issue

ChoSeoHwan avatar Jan 17 '23 06:01 ChoSeoHwan

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

gaurang171 avatar Jan 23 '23 18:01 gaurang171

Hello! Any update on this? Still can't find a way to make this work 😓

dylandjian avatar Jan 27 '23 16:01 dylandjian

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:

sancelot avatar Feb 01 '23 17:02 sancelot

Same here with nanoid version 4.0.1 :/, any news?

alejandrombc avatar Feb 28 '23 22:02 alejandrombc

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

sancelot avatar Mar 01 '23 15:03 sancelot

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.

owen26 avatar Mar 01 '23 17:03 owen26

same issue with nanoid 4.0.2. I updated the whole dependencies (nx migrate --run-migrations) and still have the error.

shayank avatar Apr 04 '23 06:04 shayank

@sancelot I made a reproduction repo showing the issue (for me at least) https://github.com/christopher-caldwell/esm-require

christopher-caldwell avatar Apr 10 '23 01:04 christopher-caldwell

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)

caioquirino avatar Apr 17 '23 19:04 caioquirino

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);
});

danielmahon avatar Apr 21 '23 22:04 danielmahon

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.

nicu-chiciuc avatar Jun 11 '23 13:06 nicu-chiciuc

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

Kolahzary avatar Jun 16 '23 12:06 Kolahzary

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

binaryartifex avatar Jul 17 '23 08:07 binaryartifex

@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 avatar Jul 25 '23 04:07 mattfysh

@mattfysh where do you change this ?

dmastag avatar Jul 26 '23 04:07 dmastag

@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

mattfysh avatar Jul 26 '23 04:07 mattfysh

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

dmastag avatar Jul 26 '23 04:07 dmastag

Somehow, I was able to get it working with the newer executors. https://github.com/christopher-caldwell/esm-require/tree/working

christopher-caldwell avatar Jul 26 '23 16:07 christopher-caldwell