Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
The envify npm package is a tool that allows you to replace environment variables in your JavaScript code with their corresponding values at build time. This is particularly useful for client-side applications where you want to inject environment-specific configurations without exposing sensitive information.
Basic Environment Variable Replacement
This feature allows you to replace environment variables in your JavaScript code with their actual values. In this example, the NODE_ENV variable is replaced with 'production' during the build process.
const envify = require('envify');
const browserify = require('browserify');
browserify('./main.js')
.transform(envify({ NODE_ENV: 'production' }))
.bundle()
.pipe(process.stdout);
Using with Gulp
This feature demonstrates how to use envify with Gulp, a popular task runner. The environment variable NODE_ENV is replaced with 'production' during the build process, and the resulting bundle is saved to the './dist' directory.
const gulp = require('gulp');
const browserify = require('browserify');
const source = require('vinyl-source-stream');
const envify = require('envify/custom');
gulp.task('build', function () {
return browserify('./main.js')
.transform(envify({ NODE_ENV: 'production' }))
.bundle()
.pipe(source('bundle.js'))
.pipe(gulp.dest('./dist'));
});
Using with Grunt
This feature shows how to use envify with Grunt, another popular task runner. The environment variable NODE_ENV is replaced with 'production' during the build process, and the resulting bundle is saved to the 'dist' directory.
module.exports = function(grunt) {
grunt.initConfig({
browserify: {
dist: {
files: {
'dist/bundle.js': ['main.js']
},
options: {
transform: [['envify', { NODE_ENV: 'production' }]]
}
}
}
});
grunt.loadNpmTasks('grunt-browserify');
grunt.registerTask('default', ['browserify']);
};
The dotenv package loads environment variables from a .env file into process.env. Unlike envify, which replaces environment variables at build time, dotenv loads them at runtime. This makes dotenv more suitable for server-side applications or development environments.
The cross-env package allows you to set environment variables across different platforms in a consistent manner. While envify is focused on replacing environment variables in the code at build time, cross-env is used to set environment variables in scripts, making it more suitable for cross-platform development.
Selectively replace Node-style environment variables with plain strings. Available as a standalone CLI tool and a Browserify v2 transform.
Works best in combination with uglifyify.
If you're using the module with Browserify:
npm install envify browserify
Or, for the CLI:
sudo npm install -g envify
envify will replace your environment variable checks with ordinary strings -
only the variables you use will be included, so you don't have to worry about,
say, AWS_SECRET_KEY
leaking through either. Take this example script:
if (process.env.NODE_ENV === "development") {
console.log('development only')
}
After running it through envify with NODE_ENV
set to production
, you'll
get this:
if ("production" === "development") {
console.log('development only')
}
By running this through a good minifier (e.g. UglifyJS2), the above code would be stripped out completely.
With browserify:
browserify index.js -t envify > bundle.js
Or standalone:
envify index.js > bundle.js
require('envify')
Returns a transform stream that updates based on the Node process'
process.env
object.
require('envify/custom')([environment])
If you want to stay away from your environment variables, you can supply your own object to use in its place:
var browserify = require('browserify')
, envify = require('envify/custom')
, fs = require('fs')
var bundle = browserify('main.js')
, output = fs.createWriteStream('bundle.js')
b.transform(envify({
NODE_ENV: 'development'
}))
b.bundle().pipe(output)
FAQs
Selectively replace Node-style environment variables with plain strings.
The npm package envify receives a total of 573,222 weekly downloads. As such, envify popularity was classified as popular.
We found that envify demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.