webpack 은 .mjs 를 javascript/esm 으로 고정해 CommonJS 를 막는다1 — type 을 직접 주면 그 기본 규칙을 건너뛴다2.

configureWebpack: {
    module: {
      rules: [
        {
          test: /\.mjs$/,
          include: /node_modules/,
          type: 'javascript/auto',
        },
      ],
    },
  },

Footnotes

  1. Module Methods | webpack: “.mjs or .js with "type": "module" in package.json” → “No CommonJS allowed, for example, you can’t use require, module.exports or exports”. webpack 4 기본 규칙: { test: /\.mjs$/i, type: "javascript/esm" } ↩

  2. Module | webpack: “Rule.type sets the type for a matching module. This prevents defaultRules and their default importing behaviors from occurring.” ↩

`#292`