Skip to content

Error: "undefined is not an object" when using "globalObject: this" and "runtimeChunk: single" #15545

@jluxenberg

Description

@jluxenberg

Bug report

What is the current behavior?

Using a combination of globalObject: 'this' and runtimeChunk: 'single' in webpack.config.js causes the following error when including the generated code in a browser:

TypeError: undefined is not an object (evaluating 'this["webpackChunkMyLibrary"]')

This issue started on Webpack v5.47.0. I believe it was caused likely caused by this commit:
0bd1e78#diff-820a04017e95cec05fd58ca44cd7f6c9cfa5b698a144471d5a0f523932486c56R619

If the current behavior is a bug, please provide the steps to reproduce.

See https://github.com/jluxenberg/webpack-use-strict-bug-report for full reproduction instructions.

My webpack.config.js:

const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist'),
    library: 'MyLibrary',
    libraryTarget: 'umd',
    globalObject: 'this',
  },
  optimization: {
    minimize: false,
    runtimeChunk: 'single',
  },
};

What is the expected behavior?

I see the text "hello world" printed to the console when including the generated runtime.js and main.js files.

Other relevant information:
webpack version: bug occurs starting with v5.47.0. I tested with latest (v5.70.0) as well.
Node.js version: N/A
Operating System: MacOS 12.1 Monterey
Additional tools:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions