Thanks to visit codestin.com
Credit goes to github.com

Skip to content

module.parser.javascript.createRequire: true does not work for ES module script (.mjs) without transpilation #20477

@jet2jet

Description

@jet2jet

Bug Description

I tried the configuration module.parser.javascript.createRequire: true along with module.parser.javascript.commonjs: true. When ES module depends to local CommonJS modules (.cjs or etc.) by using createRequire() - require(), bundling will performed correctly if non-pure ES module such as .mts uses CommonJS, but performed with error if pure ES module (without transpilation) such as .mjs uses CommonJS.

Link to Minimal Reproduction and step to reproduce

https://github.com/jet2jet/webpack-create-require-test

Steps:

  1. Run npm install or npm ci
  2. Run npm run build

Expected Behavior

Bundling process works correctly (with no errors)

Actual Behavior

An error is reported for requiring CommonJS from .mjs with message such as: Module not found: Error: Can't resolve 'module' in '/path/to/project/src'

  • In the above repository, two entry points are specified: src/ts.mts and src/js.mjs, and src/foo.cjs is imported from src/ts.mts and src/js.mjs. For src/ts.mts, bundling process works correctly as expected, but src.js.mjs not.

Environment

System:
    OS: Linux 6.6 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
    Memory: 6.99 GB / 7.58 GB
  Binaries:
    Node: 20.19.3 - /home/user/.asdf/installs/nodejs/v20.19.3/bin/node
    npm: 11.5.1 - /home/user/.asdf/installs/nodejs/v20.19.3/bin/npm
    pnpm: 8.7.1 - /home/user/.local/share/pnpm/pnpm
  Packages:
    ts-loader: ^9.5.4 => 9.5.4
    webpack: ^5.105.2 => 5.105.2
    webpack-cli: ^6.0.1 => 6.0.1

Is this a regression?

None

Last Working Version

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions