babel-plugin-dev
raw JSON → 1.0.0 verified Sat Apr 25 auth: no javascript maintenance
Simple Babel plugin that replaces the global `__DEV__` identifier with `process.env.NODE_ENV !== 'production'`, allowing developers to conditionally include debug-only code without runtime overhead. Version 1.0.0 is stable and final; no new releases expected. It is lightweight with zero dependencies, works with Babel 6 and 7, and is used in production by many React-based libraries. Unlike alternatives like babel-plugin-transform-inline-environment-variables or manual if-blocks, this plugin provides a single-purpose, semantic `__DEV__` syntax with no configuration required.
Common errors
error Error: Cannot find module 'babel-plugin-dev' ↓
cause The package is not installed or is installed globally instead of locally.
fix
Run
npm install babel-plugin-dev --save-dev in your project root. error Error: __DEV__ is not defined ↓
cause The plugin is not applied or Babel compilation is not happening.
fix
Add 'dev' to your Babel plugins list (e.g., in .babelrc) and ensure you are compiling the file with Babel.
error SyntaxError: Unexpected token (1:10) > 1 | if (__DEV__) { ↓
cause The file is not being parsed by Babel (e.g., using node directly without transpilation).
fix
Make sure to run the file through Babel before execution, e.g., using @babel/node or a build step.
Warnings
gotcha The plugin replaces the global identifier `__DEV__` in all contexts, not just in variable declarations or expressions. ↓
fix Ensure `__DEV__` is not used as a variable name for other purposes; if you need scoped dev flags, use a different approach.
gotcha The replacement is textual and does not check for reassignment. If you assign `__DEV__ = false`, the replacement still occurs. ↓
fix Do not reassign `__DEV__`. Use it only as a read-only global flag.
deprecated This plugin is in maintenance mode. It works with Babel 6 and 7 but no new features are planned. ↓
fix Consider using @babel/plugin-transform-inline-environment-variables for more flexibility, or manually check process.env.NODE_ENV.
Install
npm install babel-plugin-dev yarn add babel-plugin-dev pnpm add babel-plugin-dev Imports
- default plugin wrong
const plugin = require('babel-plugin-dev'); module.exports = { plugins: [plugin] }correctmodule.exports = { plugins: ['dev'] } - Programmatic usage with Babel core wrong
const babel = require('babel-core'); babel.transform(code, { plugins: ['dev'] })correctconst babel = require('@babel/core'); babel.transform(code, { plugins: ['dev'] }) - CLI usage wrong
babel --plugin babel-plugin-dev script.jscorrectbabel --plugins dev script.js
Quickstart
// File: test.js
const __DEV__ = true;
// In production, __DEV__ is replaced with process.env.NODE_ENV !== 'production'
if (__DEV__) {
console.log('debug info');
}
// .babelrc
{
"plugins": ["dev"]
}
// Run:
// npx babel test.js --plugins dev
// Output: if (process.env.NODE_ENV !== 'production') { console.log('debug info'); }