-
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
@nrwl/node:node executor does not support importing ESM libs #11335
Comments
+1, i have the same problem |
I have the same
|
same issue: can't import |
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 |
We are running into this same issue with |
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 |
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. I made a node application and used this import and it worked . import KcAdminClient from '@keycloak/keycloak-admin-client'; my environment : nx : 15.6.3
|
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 Also out of curiosity, I tried the new serve executor 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. |
@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:
|
My current solution for an esm module built from esbuild is to update // 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): 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 |
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....
just brought my whole initial startup deployment to a crashing halt... |
I was able to get this working by changing the build target options. use |
@mattfysh where do you change this ? |
Somehow, I was able to get it working with the newer executors. https://github.com/christopher-caldwell/esm-require/tree/working |
Interesting, I am using webtorrent and the new executioners sadly don't work for me. Even though I can see that #10414 is executed. I put the whole empty project at I do see the require-override being called, but still its not working. |
Same issue with the ESM-only library "file-type". Bummed to see this is an issue for several years now. |
@Mydayyy can you upload a repro to github and I'll take a look |
Hey, sure. You can find it here: https://github.com/Mydayyy/nx-nestjs-esm/tree/master Readme details the (fairly simple) steps to reproduce it. As far as I can see the issues stems from the fact that the import of "file-type" is transpiled to a require. I checked one of my other projects where I am using the same library and the import is transpiled differently, but I did not yet dig deeper. The repo was created with the nx workspace creation with the nest preset
|
@Mydayyy this one has me beat 😮💨 I tried a bunch of different things to try and get the output to support dynamic imports, but nothing worked
Then updated the source code to this:
And yet even with these changes (and a few others I tried) something in the nx/nest/webpack/ts stack is stubbornly transpiling the dynamic import. Is there any way you can take the nest starter template and replace webpack with esbuild, or does nest require the use of the webpack bundler? |
Yea, I was not able to find a solution either (except that ugly workaround which I commented out in my repro repo). If I ever find a solution here when circle back to the issue I'll keep you posted, for now I guess its the ugly workaround and waiting for better esm support.
Its a bit more elaborate. I think its possible but requires a decent amount of custom configuration. Currently webpack is baked into the template and not configurable |
It got fixed by including "find-cache-dir": "^3.3.2" package into the project. |
Yeah, had the same thing today. More and more libraries will switch to ESM only and Nx will break more and more and ugly workarounds like this have to be created... Where can I find the roadmap regarding Nx and ESM? Our company has to invest more and more into keeping the stuff running. |
One more with noble/ed25519 which is described as ' Pure ESM, can be imported without transpilers'. That is so sad because this library is perfect for my project... Any news? |
Have you tried the option described here? https://github.com/christopher-caldwell/esm-require/blob/255b3250325ab03616931d15c34115f73f300bfc/packages/api/project.json#L14-L15 I believe that's what made it work for me when using noble/ed25519. |
Hi @beeman I tried just now, and I still have the same issue. I noticied we don't use the same executor, maybe because I use NestJS. |
@morgan-wild if you don't NEED webpack then use esbuild as you should just need to set the format option to esm, set createPackageJson and create a partial package.json with type=module. Make sure you update your tsconfigs to use esm as well. Last time I got esm to work with webpack I had to override the webpack config with ALOT of seemingly redundant options. And working with external libs was a pain. Also haven't checked in a few months but I don't think NestJS supports esm yet. So that might be a blocker for you unless there is a workaround there too. (Replying on mobile, sorry if I'm missing additional context) |
Thank you for your reply. I hope NestJS will support it soon because I have a nice project made with it. For my issue I finally used another library but not great as noble/ed25519. |
I got my webtorrent to finally work in nestjs Its based on #18974 (comment) Sharing the Code, hopefully this can help someone :
Nodejs will spit out some warnings, but it works "for now" |
I encountered this same problem when running my project that includes the
For reference my
The My searching found this thread and I attempted these solutions from it: The structure of my file
I studied the repo for what might have made the I tried to switch executor from webpack to esbuild, and "format" from "cjs" to "esm". I encountered a condition where local imports only worked with a ".js" file extension, and could not work out how to fix this, so I couldn't get this to work. This looked NestJS-specific, and I was unsure if this translated to a more generalised solution (e.g. I don't use NestJS)
This produced a working solution, albeit a non-ideal one. Any solution that sacrifices potential module updates or fixes - particularly security fixes - is not ideal by any stretch! So the time spent trying different solutions to this problem has probably been around 4 hours. I'm not sure if this would be a typical developer experience or not in this case?!? So this raised for me some genuine questions about what seems to me to be a serious shortcoming in the Nx tooling such that ESM modules are not usable (this may be too strong a statement, but it fits my current experience - would be over the moon to be proven wrong though if I'm missing an obvious solution!). And this has been a documented and unfixed problem for around 18 months now. Although there is a healthy interest in this thread, there doesn't seem to be an urgency to fix. (a) Are there not many ESM libraries out there, so that this is not hitting many developers yet and therefore not that important yet? (despite a general consensus that ESM-style imports are preferred over CommonJS-style going forward) And none of this is a slight on Nx developers - I see 722 open issues as I type this, so hands will be very full out there, prioritisation will obviously be a necessity, and further I'm unfortunately in no position to understand let alone develop a potential PR for this! Just trying to get a bit of mental clarity on the state of play here! Sorry for the long write-up - congratulations if you made it to the end! :-) |
I think this might be a bit of a too-simple view of things, I see this as an issue with the ecosystem at large. In my case, I use my Nx to build APIs that use Nest.js, and that framework doesn't support it. The reason: fastify also doesn't support it because it's immature. So there's a chain of dependencies that make this an issue. Nx merely reflects this. I managed to get ESM working using the third fix in your list but that was not a Nest.js project. So it can work, it depends on what you want. Not a lot that Nx can do here imo. |
Thanks @beeman for your insight - it does help to get this perspective on things and is much appreciated! A few thoughts:
Yes, my view of things might be simple and simply off-base even?!? Hence my line of questioning :-) To me, this Nx error:
suggests that Nx code ( This is nothing to do with
My wants are quite simple. I want:
Here's where I'd have a slightly different take on it. A healthy number of developers have contributed text or emoji-love to comments in this thread, suggesting there is at least a reasonable interest in this topic. The Nx developers (hi!) at the very least could: I feel like I'm floundering a bit here, and I'm wondering if others are as well? |
I've got a ground-breaking idea, instead of NX constantly bring out more and more new feature's, maybe start triaging issues that are older than 12 months and FIX THEM FIRST. You can add all the new hotness you want, but what bloody good is it when folks get legitimately blocked on issues like this? ill show myself out... |
I've landed on this method of importing esm libs:
usage:
would be cool to not have to do so, but it works for now it's also how nx does it internally:
|
Interested in peoples thoughts. I've run across issues which seem similar to the above. My specific issue relating to loading buildable libraries. My observation is that the Example temporary ...
"compilerOptions": {
"paths": {
...
"@my-project/cli-lib": [
"dist/libs/cli-lib" // <-- added by Nx to reference built library
]
}
}, What it looks like Nx does is traverse the build task graph to collate the dependency references needed for this process. There is I've put together an approach which uses a custom executor
I'm interested in peoples thoughts and happy to share my executor if it would be useful. Note that the way dependencies are gathered has some sharp edges and is changing - see NX_BUILDABLE_LIBRARIES_TASK_GRAPH which seems to the intended path forward (see here, here and here). |
In case it is useful, I've published a Node executor including ESM module resolution for buildable libraries within Nx workspaces. |
Hi, I have the same issue - thanks for sharing your executor @danielsharvey. I tried to implement it by updating my project.json:
Sadly I'm still getting the same error, perhaps I am missing something? nx: 17.3.2 If its any use I believe nodemailer is causing the issue. Any additional insight is appreciated. Thanks |
@niallconaghan Can you say more about the failure / error you are seeing? nodemail publishes a CommonJS library from my look - how are you require'ing that into your library/app? |
Hope this will help someone :) "serve": {
"executor": "nx:run-commands",
"dependsOn": ["build"],
"defaultConfiguration": "development",
"options": {
"watch": true,
"command": "concurrently -n \"runner,builder-watcher\" \"pnpm exec tsx --watch dist/apps/g3-worker/main.js\" \"pnpm exec nx watch --projects=g3-worker -d -- pnpm exec nx build g3-worker\""
},
"configurations": {
"development": {},
"production": {}
}
}, |
@danielsharvey Your library works well for serve. How do we make it work for build? I built your sample app using Then ran Getting the below error
|
No solution
|
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
@nrwl/node
application.yarn add @keycloak/keycloak-admin-client
main.ts
nx serve
nx build
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: #10296
The text was updated successfully, but these errors were encountered: