liady / webpack-node-externals

Easily exclude node modules in Webpack
MIT License
1.3k stars 62 forks source link

README confusingly advises removing "target: 'node'" for Webpack 5 #104

Closed toolness closed 3 years ago

toolness commented 3 years ago

I recently upgraded from Webpack 4 to 5 and followed the instructions in the README, added in 6dc8265e24788b70add4e2bf50fd1a13581d18af:

Note: For Webpack 5, replace target: 'node' with the externalsPreset object:

However, I found that the bundle created used the browser versions of third-party modules that it ended up including (e.g. the ones I specified in my allowlist), rather than the node-based versions. An example of this is cuid, which included its fingerprint.browser.js (and which threw an exception upon import, since it expected a browser environment and didn't find one).

Once I re-added target: 'node' to my webpack configuration, the bundle no longer used the browser versions of dependencies, and everything worked okay.

So I might be wrong here, but maybe better advice is to add the externalsPreset object in addition to keeping target: 'node'?

liady commented 3 years ago

@toolness you're right, the externalsPreset should come as a complement to target: 'node', and not replace it. I've fixed the README. Thank you.

toolness commented 3 years ago

Oh cool! Thanks @liady!