is-webpack-bundle
raw JSON → 1.0.0 verified Sat Apr 25 auth: no javascript maintenance
A micro-package (v1.0.0, no active releases since 2019) that detects at runtime whether the current module was bundled by webpack. It works by checking for the presence of a `__webpack_require__` function in the global scope. This is a simple, single-purpose utility with no dependencies. For similar detection in other bundlers (e.g., Rollup, Parcel), alternative approaches are needed. The package is published as a CommonJS module only, with no TypeScript type definitions.
Common errors
error Cannot find module 'is-webpack-bundle' ↓
cause Package not installed or typo in package name.
fix
Run npm install is-webpack-bundle
error Uncaught ReferenceError: __webpack_require__ is not defined ↓
cause The package's global check fails because __webpack_require__ is not exposed (e.g., in Node.js environment without webpack).
fix
This error should not happen because the package catches the ReferenceError internally; if it does, ensure you are not modifying global scope or using eval.
error SyntaxError: Cannot use import statement outside a module ↓
cause Trying to ESM-import the package without proper module configuration.
fix
Use require() or set "type": "module" in package.json and ensure the import path is valid.
Warnings
gotcha The check relies on a global `__webpack_require__` which may be absent in certain webpack configurations (e.g., output.libraryTarget: 'var') or minified bundles. ↓
fix Verify the check works in your specific webpack setup; consider alternative detection methods.
gotcha If the code is bundled with another bundler (e.g., Rollup, Parcel), the check will incorrectly return false even if inside a bundle. ↓
fix Use a bundler-agnostic detection library or check for other globals.
gotcha The module is CommonJS-only; importing via ESM may cause issues in strict ESM environments. ↓
fix Use require() or configure your bundler to handle CJS interop.
Install
npm install is-webpack-bundle yarn add is-webpack-bundle pnpm add is-webpack-bundle Imports
- default wrong
import isWebpackBundle from 'is-webpack-bundle';correctconst isWebpackBundle = require('is-webpack-bundle'); - default (ESM) wrong
const isWebpackBundle = require('is-webpack-bundle');correctimport isWebpackBundle from 'is-webpack-bundle'; - default (TypeScript) wrong
import isWebpackBundle from 'is-webpack-bundle';correct// @ts-ignore or declare module const isWebpackBundle = require('is-webpack-bundle');
Quickstart
const isWebpackBundle = require('is-webpack-bundle');
if (isWebpackBundle) {
console.log('Running inside a webpack bundle');
} else {
console.log('Not bundled by webpack');
}
// Example usage: conditionally handle module exports
const myModule = isWebpackBundle
? { bundled: true }
: { bundled: false };
module.exports = myModule;