Envify: Environment Variable Replacement

4.1.0 · maintenance · verified Wed Apr 22

Envify is a Browserify transform that selectively replaces Node.js-style environment variables (e.g., `process.env.NODE_ENV`) with plain string literals during the bundling process. This optimization is crucial for front-end applications, as it allows tools like UglifyJS to perform dead-code elimination, significantly reducing bundle size by stripping out environment-specific code paths (e.g., development-only logging). The package is currently at version 4.1.0 and, while mature and stable, its release cadence is infrequent, reflecting its maintenance status rather than active development. Key differentiators include its tight integration with Browserify's transform pipeline and its ability to purge unused `process.env` references to avoid including Browserify's `process` shim, further optimizing bundle size. It supports both using the system's `process.env` or providing a custom environment object.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `envify` programmatically with Browserify to replace environment variables. It shows both a production build (where `NODE_ENV` is 'production' and a feature flag is 'disabled', leading to dead-code elimination) and a development build (where specific code paths are retained).

import browserify from 'browserify';
import envify from 'envify';
import fs from 'node:fs';
import path from 'node:path';

const mainJsContent = `
if (process.env.NODE_ENV === "development") {
  console.log('development only debug log');
}

if (process.env.FEATURE_FLAG === "enabled") {
  console.log('Feature A is enabled!');
}

console.log('This always runs.');
`;

const outputDir = path.join(process.cwd(), 'temp_build');
if (!fs.existsSync(outputDir)) {
  fs.mkdirSync(outputDir);
}
const mainJsPath = path.join(outputDir, 'main.js');
fs.writeFileSync(mainJsPath, mainJsContent);

// Bundle for production
const b = browserify(mainJsPath);
b.transform(envify({ 
  NODE_ENV: 'production',
  FEATURE_FLAG: 'disabled',
  _: 'purge' // Purge undefined process.env references
}));

const productionBundlePath = path.join(outputDir, 'bundle.production.js');
b.bundle().pipe(fs.createWriteStream(productionBundlePath))
  .on('finish', () => {
    console.log(`Production bundle created at: ${productionBundlePath}`);
    console.log('Content (simplified):');
    console.log(fs.readFileSync(productionBundlePath, 'utf8').substring(0, 200) + '...');
    // Expected: console.log('This always runs.'); without development/feature logs
  });

// Example of development bundle (for demonstration, showing code NOT removed)
const bDev = browserify(mainJsPath);
bDev.transform(envify({ 
  NODE_ENV: 'development',
  FEATURE_FLAG: 'enabled'
}));

const developmentBundlePath = path.join(outputDir, 'bundle.development.js');
bDev.bundle().pipe(fs.createWriteStream(developmentBundlePath))
  .on('finish', () => {
    console.log(`\nDevelopment bundle created at: ${developmentBundlePath}`);
    console.log('Content (simplified):');
    console.log(fs.readFileSync(developmentBundlePath, 'utf8').substring(0, 200) + '...');
    // Expected: both console.logs visible
  });

view raw JSON →