-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
nodejs esm module output bundling lacks module prototype for externals #7446
Comments
The original code is written using commonjs and without extra setup it is impossible to solve, because that's how esm work (we are unable to determine how the module is used in code and by default we use namespace import and so the prototype is lost), solutions:
externals: function ({ request }, callback) {
if (/^node:/.test(request) || builtinModules.includes(request)) {
return callback(null, 'node-commonjs ' + request);
}
callback();
}, Feel free to feedback |
@snitin315 We should improve import { createRequire as __WEBPACK_EXTERNAL_createRequire } from "node:module";
// ...
/***/ 2613:
/***/ ((module) => {
module.exports = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("stream");
/***/ }), This allows you to solve problems when you have: // ...
function ChunkStream() {
Stream.call(this);
}
util.inherits(ChunkStream, Stream); |
I'll update the docs 👍 |
Wow thank you so much for the swift and detailed response! I can confirm it working for my case. I think it's on me sleeping on |
Let's keep open, we need update docs in such cases |
Bug report
What is the current behavior?
For my electron app I want to bundle the nodejs code of my main thread because of a) typescript and b) performance optimizations. That means I have
"type": "module"
, regular node 20+ esm style code and want to bundle my dependencies into a final bundle (as I ship this bundle with my electron executable and not as part of a backend that has a node_modules dir).To achieve a clean esm bundle (without createRequire and with support for dynamic imports) I use:
I now want to bundle in the
jimp
library that has thepngjs
dependency, which uses anutil.inherits()
call onto node-nativestream
in theirchunkstream.js
file:https://github.com/pngjs/pngjs/blob/c565210c602527eb459f857eeb78183997482d5b/lib/chunkstream.js#L18
It looks like this in the bundle:
Now the issue is that util.inherits expects
stream
to have a prototype and errors like so when run:When I edit the bundle, manually logging
Stream.prototype
yieldsundefined
, and loggingStream
yields:I'm assuming the prototype that
util.inherit
expects got lost during webpacks bundling.If the current behavior is a bug, please provide the steps to reproduce.
Here's a minimal repo for reproduction: https://github.com/tom2strobl/webpack-jimp-nodenext
See for additional comments regarding the webpack configuration: https://github.com/tom2strobl/webpack-jimp-nodenext/blob/main/webpack.config.js
What is the expected behavior?
webpack to bundle in a way that standard node modules that are defined as external retain their original prototypes for
util.inherits
compatibility.Other relevant information:
webpack version: 5.95
Node.js version: 20.17
Operating System: macOS 15.0.1
Additional tools: jimp 1.6.0 / pngjs 7.0.0
The text was updated successfully, but these errors were encountered: