When running my webpack config for an express server that uses ES6 modules, has "type": "module" defined in package.json and uses target: 'node' it fails with an an error ReferenceError: require is not defined.
When looking at the processed javascript file i see the following statements that cause this error:
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ({
/***/ "body-parser":
/*!******************************!*\
!*** external "body-parser" ***!
\******************************/
/***/ ((module) => {
module.exports = require("body-parser");
/***/ }),
Why does webpack include this code? The server runs totally fine when not processed with webpack.
I solved it by adding
experiments: {
...
outputModule: true,
},
output: {
...
chunkFormat: 'module',
}
To my webpack.config.js