Skip to content
Advertisement

Webpack 5 and ESM

I think I’ve read every thread on SO and every related page on the internet on this, everything has some variation of a problem

I want:

  • To use webpack to bundle my web app up
  • To use ES Modules within my source js and have them transpiled down for wider browser support
  • To use ES Modules within my webpack configuration

Node 14 allegedly supports ESM, so lets use that

Setup 1

I have "type": "module" in my package.json

then my webpack.config.js looks something like:

JavaScript

running > webpack (webpack-cli) I get:

JavaScript

OK, so lets do what the error message says

Setup 2a

If I remove "type": "module" from my package.json I get

JavaScript

right…. So lets try the other suggested alternative:

Setup 2b

JavaScript

I get a segfault.

/c/Program Files/nodejs/webpack: line 14: 14272 Segmentation fault "$basedir/node" "$basedir/node_modules/webpack/bin/webpack.js" "$@"

Advertisement

Answer

At the time of writing, webpack-cli just doesn’t support ES6 modules, so you basically have to re-implement it yourself.

It’s not that hard really, just annoying. You need something like this (simplified for brevity): Just RTFM here https://webpack.js.org/api/node/

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement