How to setup the pure ES modules loading with "webpack-node-externals" #19102
Unanswered
TokugawaTakeshi
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Hi, don't know if I got your issue but I hit something similar yesterday. After couple of hours of research, I found a way to use In my webpack.js I have:
basically I found this option that is impacting how webpack-node-external generate the code for importing the dependencies. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I am asking this question after has not been answered to similar one in Stack Overflow despite the fact that I have started the bounty and created the reproduction. Hope this question will be answered here.
Remark
Weird things are happening with modules types is Node.js.
On the one hand, the CommonJS modules are still (for the end of 2024) default in Node.js.
On the other hand, the agitating about migrating to ECMAScript modules is in process.
However, this migration is not simple in usually will not ends by replacing of
require()
toimport
and adding of"type": "module"
to the package.json. So, because a large number of npm libraries are still CommonJS, installing the various decencies to our project usually we have the mashup of CommonJS modules and ECMAScript modules:In gulp-imagemin, there is the issue titled 8.0.0 - ESM only? Serious?.
Maybe it is outdated (Aug 2021), but I still facing with ESM incompatibility errors using all Webpack, tsx and ts-node. Well, this question about Webpack, so time to return to the subject of this question.
My Case
I am creating the project building tool, but to build its own source code, the Webpack is being used.
AFAIK:
Particularly, the
target: "node"
affects on which modules will be at the output.So, if to make the previous scheme more detailed, besides the output entry point there are will be the plenty of non-bundled node_modules:
The building of entry point going well, but if to try to execute the built entry point, the error like the following one will be:
Some specific packages.
Reproduction
The reproduction includes the cases with imagemin + imagemin-pngquant and w3c-html-validator.
If you will be so kind to checkout this GitHub repository, all you need to get the above error is execute
for imagemin + imagemin-pngquant test, and
for the w3c-html-validator. In both cases, you will get the "Error [ERR_REQUIRE_ESM]: require() of ES Module" error.
If you want to correct the source TypeScript code, please execute
to rebuild the output JavaScript code.
Running away from the problems
The following measures are the workarounds rather than solution.
Use the dynamic import
Depending on the application, it could cause the large-scale refactoring.
For example, assume that previously the functionality imported synchronously is being used in the classes' constructors. But the class constructor could not be async. Thus, we must do something, for example, split the initialization to 2 steps.
For the Gulp case, most of Gulp task functions (especially the ones which are using the plugins) returns the streams, not promises. Unfortunately, the combining of promises and streams is not simple in the Gulp.
Well, even if above problems are solvable, it was just the example. But generally, the starting of the usage of the dynamic import could cause the large-scale changes in the code.
Set
type: module
to the package.jsonI can not understand hot it could be recommended such simply.
The adding of
type: module
to "package.json" could and most likely will brake both project and what depends on it!.So, unfortunately it is not that could be simply done.
Beta Was this translation helpful? Give feedback.
All reactions