{"id":16807,"library":"envify","title":"Envify: Environment Variable Replacement","description":"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.","status":"maintenance","version":"4.1.0","language":"javascript","source_language":"en","source_url":"git://github.com/hughsk/envify","tags":["javascript","environment","variables","browserify","browserify-transform","transform","source","configuration"],"install":[{"cmd":"npm install envify","lang":"bash","label":"npm"},{"cmd":"yarn add envify","lang":"bash","label":"yarn"},{"cmd":"pnpm add envify","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Envify is primarily used as a transform for Browserify to process source code.","package":"browserify","optional":false},{"reason":"Often used in conjunction with Uglifyify (or other minifiers) to enable dead-code elimination after environment variable replacement.","package":"uglifyify","optional":true}],"imports":[{"note":"While Browserify itself supports CJS, `envify`'s module usage (when passed as a transform) is often done via `require`. For direct module imports in modern JS, `import` is preferred, though direct programmatic usage with Browserify typically involves `require` for transforms. The `envify` package itself exports a default function for the transform stream.","wrong":"const envify = require('envify');","symbol":"envify","correct":"import envify from 'envify';"},{"note":"To provide a custom environment object instead of relying on `process.env`, import from the `envify/custom` submodule. This is a named export from the perspective of modern module systems, but a direct path in CJS.","wrong":"const customEnvify = require('envify').custom;","symbol":"customEnvify","correct":"import customEnvify from 'envify/custom';"},{"note":"When using `envify` programmatically with Browserify, you usually call `envify()` (or `envify/custom(opts)`) to get the transform function/stream, rather than passing the string name 'envify'.","wrong":"b.transform('envify');","symbol":"EnvifyTransform","correct":"b.transform(envify());"}],"quickstart":{"code":"import browserify from 'browserify';\nimport envify from 'envify';\nimport fs from 'node:fs';\nimport path from 'node:path';\n\nconst mainJsContent = `\nif (process.env.NODE_ENV === \"development\") {\n  console.log('development only debug log');\n}\n\nif (process.env.FEATURE_FLAG === \"enabled\") {\n  console.log('Feature A is enabled!');\n}\n\nconsole.log('This always runs.');\n`;\n\nconst outputDir = path.join(process.cwd(), 'temp_build');\nif (!fs.existsSync(outputDir)) {\n  fs.mkdirSync(outputDir);\n}\nconst mainJsPath = path.join(outputDir, 'main.js');\nfs.writeFileSync(mainJsPath, mainJsContent);\n\n// Bundle for production\nconst b = browserify(mainJsPath);\nb.transform(envify({ \n  NODE_ENV: 'production',\n  FEATURE_FLAG: 'disabled',\n  _: 'purge' // Purge undefined process.env references\n}));\n\nconst productionBundlePath = path.join(outputDir, 'bundle.production.js');\nb.bundle().pipe(fs.createWriteStream(productionBundlePath))\n  .on('finish', () => {\n    console.log(`Production bundle created at: ${productionBundlePath}`);\n    console.log('Content (simplified):');\n    console.log(fs.readFileSync(productionBundlePath, 'utf8').substring(0, 200) + '...');\n    // Expected: console.log('This always runs.'); without development/feature logs\n  });\n\n// Example of development bundle (for demonstration, showing code NOT removed)\nconst bDev = browserify(mainJsPath);\nbDev.transform(envify({ \n  NODE_ENV: 'development',\n  FEATURE_FLAG: 'enabled'\n}));\n\nconst developmentBundlePath = path.join(outputDir, 'bundle.development.js');\nbDev.bundle().pipe(fs.createWriteStream(developmentBundlePath))\n  .on('finish', () => {\n    console.log(`\\nDevelopment bundle created at: ${developmentBundlePath}`);\n    console.log('Content (simplified):');\n    console.log(fs.readFileSync(developmentBundlePath, 'utf8').substring(0, 200) + '...');\n    // Expected: both console.logs visible\n  });","lang":"javascript","description":"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)."},"warnings":[{"fix":"Upgrade Browserify to version 3.25.0 or higher, or use the module API for programmatic control over environment variables and purging.","message":"Older versions of Browserify might not support the subarg syntax for passing custom environment variables or the `purge` option directly via the CLI. Ensure Browserify is at least v3.25.0 for full CLI feature support.","severity":"breaking","affected_versions":"<3.25.0 of browserify"},{"fix":"To prevent the `process` shim from being included for undefined variables, use the `purge` option by passing `_:'purge'` to the module API or `envify purge` to the CLI command (e.g., `browserify -t [ envify purge --NODE_ENV production ]`).","message":"By default, `envify` only replaces environment variables that are explicitly defined in the `process.env` object or passed to `envify/custom`. Undefined variables are left as `process.env.VAR_NAME`, potentially causing Browserify to include its ~2KB `process` shim in the bundle.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use string comparisons (e.g., `process.env.VAR === 'true'`) or parse the environment variable string to the desired type (e.g., `parseInt(process.env.VAR, 10)`) before comparison, ensuring consistency with the expected string literal replacement.","message":"Environment variables are replaced with their *string* values. Comparisons like `process.env.VAR === 1` will become `'value' === 1`, which evaluates differently than `undefined === 1`. Always ensure your comparisons match the string replacement behavior.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Consistently use the `process.env.VAR_NAME` dot notation for all environment variable accesses that you intend `envify` to transform. For more complex access patterns, consider pre-processing your code or ensuring `envify` is the last transform before minification to catch any remaining `process.env` references for purging.","message":"Envify only processes `process.env.VAR_NAME` syntax. If you are accessing environment variables in other ways (e.g., `process.env['VAR_NAME']` or `const env = process.env; env.VAR_NAME`), these patterns might not be transformed, leading to unexpected behavior or larger bundle sizes.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure `envify` is correctly applied as a Browserify transform. Use the `purge` option (e.g., `-t [ envify purge --NODE_ENV production ]` or `{ _: 'purge' }` in the API) to replace any remaining `process.env.VAR` references with `undefined`. Avoid direct references to `process` in browser code unless specifically polyfilled.","cause":"The `process` object is a Node.js global. In a browser environment without `envify` replacing `process.env` references, Browserify includes a shim for `process` by default. If `envify` hasn't fully replaced all `process.env` references and the `purge` option isn't used, or if `process` is accessed directly for non-environment variables, this error can occur.","error":"ReferenceError: process is not defined"},{"fix":"Run `npm install envify` or `yarn add envify` in your project directory. If using it globally via CLI, run `npm install -g envify`.","cause":"The `envify` package is not installed or not discoverable in your project's `node_modules`.","error":"Error: Cannot find module 'envify'"}],"ecosystem":"npm","meta_description":null}