Skip to content
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

[Bug] ESM require error importing webpack config #128

Open
1 task
rbtcollins opened this issue Mar 19, 2022 · 1 comment
Open
1 task

[Bug] ESM require error importing webpack config #128

rbtcollins opened this issue Mar 19, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@rbtcollins
Copy link

  • 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.

@rbtcollins rbtcollins added the bug Something isn't working label Mar 19, 2022
@unlight
Copy link

unlight commented Apr 30, 2022

Rename webpack.config.js to webpack.config.cjs (fixed issue for me)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants