webpack-mode
raw JSON → 1.1.0 verified Sat Apr 25 auth: no javascript
Get the current mode (production/development) of a Webpack 4 build. Version 1.1.0 is the latest stable release. This tiny utility checks the `--mode` CLI flag or `NODE_ENV` environment variable and exposes `isProduction`, `isDevelopment`, and a string-comparable object. Works only with Webpack 4 — it will silently return wrong results on Webpack 5. No dependencies, no TypeScript types.
Common errors
error Cannot find module 'webpack-mode' ↓
cause Package not installed or installed as a devDependency but used in production code.
fix
Run 'npm install --save-dev webpack-mode' or 'yarn add --dev webpack-mode'.
error TypeError: webpackMode is not a string ↓
cause Using strict equality (===) on the default export.
fix
Use
if (${webpackMode} === 'production') instead of if (webpackMode === 'production'). error Property 'isProduction' does not exist on type 'typeof import("webpack-mode")' ↓
cause TypeScript cannot find typings for the package.
fix
Add a custom type declaration file (.d.ts) as shown in the warnings.
Warnings
gotcha webpack-mode works only with Webpack 4. On Webpack 5, the mode detection will likely fail or return incorrect results. ↓
fix Use Webpack's built-in mode detection or upgrade to a Webpack 5-compatible library.
gotcha The default export is not a string; strict equality (===) with a string will always be false. ↓
fix Use template literals (`${mode}`) or the .toString() method, or use the named exports isProduction/isDevelopment.
gotcha No TypeScript type definitions available; using with TypeScript will require manual declare module. ↓
fix Create a .d.ts file: declare module 'webpack-mode' { export const isProduction: boolean; export const isDevelopment: boolean; const mode: { toString(): string }; export default mode; }
Install
npm install webpack-mode yarn add webpack-mode pnpm add webpack-mode Imports
- isProduction wrong
const { isProduction } = require('webpack-mode').defaultcorrectconst { isProduction } = require('webpack-mode') - isDevelopment wrong
const isDevelopment = require('webpack-mode').isDevelopmentcorrectconst { isDevelopment } = require('webpack-mode') - default export (the mode value) wrong
const mode = require('webpack-mode').defaultcorrectconst mode = require('webpack-mode')
Quickstart
const { isProduction, isDevelopment } = require('webpack-mode');
if (isProduction) {
console.log('Building for production');
} else if (isDevelopment) {
console.log('Building for development');
} else {
console.log('Mode not set');
}
// Or get the mode as a string-comparable object:
const mode = require('webpack-mode');
if (`${mode}` === 'production') {
console.log('Mode is production');
}