config-webpack
raw JSON → 1.0.5 verified Sat Apr 25 auth: no javascript maintenance
config-webpack (v1.0.5) is a Webpack plugin that bridges node-config into client-side bundles. It replaces global references (default `CONFIG`) with resolved config values via Webpack's DefinePlugin, supporting deployment-, instance-, local files, and environment variables. Compatible with Webpack 1–4 and node-config 1–3. Unlike runtime config solutions, it bakes config at build time, making it immutable and suitable for single-page apps. The plugin only injects referenced keys, but beware that top-level objects like `CONFIG` can leak the entire config even if gated behind a condition.
Common errors
error Error: Cannot find module 'config-webpack' ↓
cause config-webpack is not installed or not in node_modules.
fix
Run npm install config-webpack in your project root.
error TypeError: ConfigWebpackPlugin is not a constructor ↓
cause Importing as default import in ESM context, but package is CommonJS.
fix
Use const ConfigWebpackPlugin = require('config-webpack');
error ReferenceError: CONFIG is not defined ↓
cause ConfigWebpackPlugin not added to webpack plugins array, or webpack config not processed.
fix
Ensure new ConfigWebpackPlugin() is in your plugins array and the config is used by webpack.
error Module build failed: Error: Cannot find module 'config' ↓
cause Missing peer dependency config.
fix
Run npm install config.
Warnings
gotcha Using CONFIG directly (e.g. console.log(CONFIG)) leaks the entire config object even if inside a false conditional, because Webpack replaces CONFIG before tree-shaking. ↓
fix Only reference specific keys like CONFIG.myKey; avoid logging or spreading the CONFIG object.
gotcha Config is immutable after replacement. Attempting to assign like CONFIG.key = value throws an error. ↓
fix Treat CONFIG as readonly; do not reassign properties.
breaking Webpack 5 is not supported. The peer dependency only allows webpack 1–4. ↓
fix Stick to webpack 4 or consider alternatives like @mhassan1/webpack-node-config.
deprecated Package is effectively unmaintained: last release 2019, no updates for webpack 5 or ESM. ↓
fix Evaluate maintenance status; consider switching to a modern alternative.
Install
npm install config-webpack yarn add config-webpack pnpm add config-webpack Imports
- ConfigWebpackPlugin wrong
import ConfigWebpackPlugin from 'config-webpack';correctconst ConfigWebpackPlugin = require('config-webpack'); - CONFIG wrong
import { CONFIG } from 'config-webpack';correctif (CONFIG.myKey) { … } - new ConfigWebpackPlugin() wrong
plugins: [ConfigWebpackPlugin()]correctplugins: [new ConfigWebpackPlugin()]
Quickstart
// webpack.config.js
const ConfigWebpackPlugin = require('config-webpack');
const path = require('path');
// Ensure config files exist
// config/default.json: { "apiUrl": "http://localhost:3000" }
// config/production.json: { "apiUrl": "https://api.example.com" }
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
plugins: [
new ConfigWebpackPlugin(),
],
};
// src/index.js
console.log('API URL:', CONFIG.apiUrl);
// In dev: 'http://localhost:3000'
// In production build: 'https://api.example.com'