webpack-global-object-x

raw JSON →
1.0.1 verified Sat Apr 25 auth: no javascript maintenance

Utility to provide the correct global object for webpack's globalObject configuration, supporting Node.js, browsers, and Web Workers. Current stable version is 1.0.1, released with no updates since 2018. It returns a function that, when called, yields the global object (`global`, `self`, or `this`). Lightweight (no dependencies) and intended for use in webpack configuration to set `output.globalObject` for UMD bundles. Differentiators: simple, focused, and independent of any runtime environment detection library.

error Error: Cannot find module 'webpack-global-object-x'
cause Package not installed or typo in package name.
fix
Run npm install webpack-global-object-x --save-dev
error Uncaught TypeError: globalObject is not a function
cause Using the imported function without calling it.
fix
Change code to: const globalObject = require('webpack-global-object-x'); const obj = globalObject();
error Module not found: Error: Can't resolve 'webpack-global-object-x' in ...
cause Attempting to import in a browser environment where require is not available.
fix
Use a bundler that supports CommonJS or pre-compile the package with your build tool.
gotcha The module exports a function, not the global object itself. You must call it to get the global object.
fix Use globalObject() to obtain the actual global object.
gotcha No ESM export; cannot be imported with named import syntax in Node.js without CJS interop.
fix Use require() or enable interop (e.g., createRequire/import from CommonJS).
gotcha The package is not actively maintained; last release was in 2018. May not support future webpack versions or newer JS environments.
fix Consider alternatives like using built-in globalThis (available in Node 12+, modern browsers) or the 'global' npm module.
npm install webpack-global-object-x
yarn add webpack-global-object-x
pnpm add webpack-global-object-x

Shows how to use webpack-global-object-x in a webpack config to set the globalObject option for UMD bundles.

// webpack.config.js
const globalObject = require('webpack-global-object-x');

module.exports = {
  output: {
    globalObject: '(' + globalObject.toString() + '())',
    library: 'MyLib',
    libraryTarget: 'umd'
  }
};