Skip to content

__webpack_module__ is rewritten to module, causing identifier collision with user-defined const module #20264

@SyMind

Description

@SyMind

Bug Description

When using __webpack_module__.id in source code, webpack rewrites it to module.id and generates a module factory with a function parameter named module. If the user also declares a variable named module, this creates a name collision/illegal redeclaration.

Source:

console.log(__webpack_module__.id);
const module = 'layout';
module;

Bundled output (excerpt):

/***/ ((module) => {

console.log(module.id);
const module = 'layout';
module;

/***/ })

This results in the user’s const module conflicting with the generated function parameter module (either a runtime bug or a SyntaxError due to redeclaration).

Uncaught SyntaxError: Identifier 'module' has already been declared

Link to Minimal Reproduction and step to reproduce

https://github.com/SyMind/webpack-module-collision

  1. pnpm install
  2. pnpm serve

Expected Behavior

Bundled output (excerpt):

/***/ ((__webpack_module__) => {

console.log(__webpack_module__.id);
const module = 'layout';
module;

/***/ })

Actual Behavior

Bundled output (excerpt):

/***/ ((module) => {

console.log(module.id);
const module = 'layout';
module;

/***/ })

Environment

System:
    OS: macOS 14.0
    CPU: (10) arm64 Apple M1 Pro
    Memory: 105.95 MB / 32.00 GB
  Binaries:
    Node: 22.20.0 - /Users/bytedance/.nvm/versions/node/v22.20.0/bin/node
    Yarn: 1.22.22 - /Users/bytedance/.nvm/versions/node/v22.20.0/bin/yarn
    npm: 10.9.3 - /Users/bytedance/.nvm/versions/node/v22.20.0/bin/npm
    pnpm: 10.23.0 - /Users/bytedance/.nvm/versions/node/v22.20.0/bin/pnpm
  Browsers:
    Chrome: 143.0.7499.147
    Firefox: 142.0.1
  Packages:
    html-loader: ^5.1.0 => 5.1.0 
    html-webpack-plugin: ^5.6.5 => 5.6.5 
    webpack: ^5.104.1 => 5.104.1 
    webpack-cli: ^6.0.1 => 6.0.1 
    webpack-dev-server: ^5.2.2 => 5.2.2

Is this a regression?

None

Last Working Version

No response

Additional Context

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions