What is node-environment-flags?
The node-environment-flags npm package is designed to manage and parse environment flags in Node.js applications. It provides a straightforward way to handle environment-specific configurations and settings, making it easier to manage development, testing, and production environments.
What are node-environment-flags's main functionalities?
Parsing Environment Flags
This feature allows developers to parse environment flags easily. The code sample demonstrates how to retrieve the value of the 'NODE_ENV' environment variable using the node-environment-flags package.
const flags = require('node-environment-flags');
console.log(flags.get('NODE_ENV'));
Setting Environment Flags
This feature enables developers to set environment flags programmatically. The code sample shows how to set the 'DEBUG' environment variable to true.
const flags = require('node-environment-flags');
flags.set('DEBUG', true);
Other packages similar to node-environment-flags
dotenv
Dotenv is a popular npm package that loads environment variables from a .env file into process.env. It is similar to node-environment-flags in managing environment-specific settings but differs as it focuses on loading variables from files rather than parsing or setting them programmatically.
cross-env
Cross-env allows you to set and use environment variables across platforms. It is similar to node-environment-flags in that it helps manage environment settings, but it is specifically designed to handle cross-platform differences in setting environment variables.
node-environment-flags
Polyfill/shim for process.allowedNodeEnvironmentFlags
node-environment-flags is a rough polyfill and shim for process.allowedNodeEnvironmentFlags, which was introduced in Node.js v10.10.0.
Table of Contents
Install
Requires Node.js v6.0.0 or newer.
$ npm i node-environment-flags
Usage
If the current Node.js version is v10.10.0 or newer, the native implementation will be provided instead.
As Polyfill (Recommended)
const nodeEnvironmentFlags = require('node-environment-flags');
nodeEnvironmentFlags.has('--require');
As Shim
require('node-environment-flags/shim')();
process.allowedNodeEnvironmentFlags.has('--require');
Notes
- This module approximates what
process.allowedNodeEnvironmentFlags
provides in versions of Node.js prior to v10.10.0. Since process.allowedNodeEnvironmentFlags
is based on NODE_OPTIONS
(introduced in v8.0.0), the set of supported flags for versions older than v8.0.0 is highly theoretical. - Version ranges are matched using semver.
- This module is granular to the minor Node.js version number; patch version numbers are not considered.
- Results for unmaintained (odd) versions of Node.js are based on data for the most recent LTS version; e.g., running this module against Node.js v7.10.0 will yield the same results as would v6.14.0.
- Prior art: @ljharb's util.promisify
Maintainers
@boneskull
License
Copyright © 2018 Christopher Hiller. Licensed Apache-2.0.