[Bug] ESM require error importing webpack config
- [ ] I'd be willing to submit the fix
Describe the bug
I have some ES module based code I wanted to test. Mocha works, but the mochapack features sounded nice, so went to try it.
First hurdle was the requirement to set ecma on the terser plugin, as 'npm exec mochapack' was throwing an error from terser.
So I created a webpack config:
$ cat webpack.config.js
const TerserPlugin = require("terser-webpack-plugin");
module.exports = {
optimization: {
minimize: true,
minimizer: [new TerserPlugin({
terserOptions: { ecma: 2020 }
})],
},
};
That got past terser, and then it throws in mochapack:
$ npm exec mochapack
<root>\node_modules\mochapack\lib\cli\argsParser\optionsFromParsedArgs\webpack\requireWebpackConfig.js:139
config = require(requirePath); // eslint-disable-line global-require, import/no-dynamic-require
^
Error [ERR_REQUIRE_ESM]: require() of ES Module <root>\webpack.config.js from <root>\node_modules\mochapack\lib\cli\argsParser\optionsFromParsedArgs\webpack\requireWebpackConfig.js not supported.
webpack.config.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename webpack.config.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in <root>\package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
mochapack was installed with ```npm installafter settingtype:'module'` in package.js.
To Reproduce
Sorry, this is a drive by, though if you can't trivially reproduce I can upload something after the semester ends.
Screenshots
If applicable, add screenshots to help explain your problem.
Environment if relevant (please complete the following information):
- OS: Windows
- Node version 17.7.2
├── chai@npm:@esm-bundle/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]
Additional context
Add any other context about the problem here.
Rename webpack.config.js to webpack.config.cjs (fixed issue for me)