Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

processenv

Package Overview
Dependencies
Maintainers
5
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

processenv

processenv parses environment variables.

  • 3.0.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
24K
decreased by-10.63%
Maintainers
5
Weekly downloads
 
Created
Source

processenv

processenv parses environment variables.

Status

CategoryStatus
Versionnpm
DependenciesDavid
Dev dependenciesDavid
BuildGitHub Actions
LicenseGitHub

Installation

$ npm install processenv

Quick start

First you need to integrate processenv into your application:

const { processenv } = require('processenv');

If you use TypeScript, use the following code instead:

import { processenv } from 'processenv';

Then, to parse an environment variable, call the processenv function and provide the name of the environment variable you would like to parse:

const port = processenv('PORT');

Please note that the value is automatically converted to the appropriate data type, e.g. a number. This also works for stringified JSON objects, in case you want to store complex configuration data inside an environment variable.

Using default values

If you want to provide a default value, you may add it as a second parameter. This also works for booleans and all other types. If neither the environment variable nor the desired default value are set, processenv returns undefined:

const port = processenv('PORT'3000);
const user = processenv('USER', 'Jane Doe');
const isRoot = processenv('ROOT'true);

Alternatively, you may also provide a function which returns the default values. This is useful, e.g. if you want to lazily evaluate a value:

const port = processenv('PORT', () => 3000);

If you want to use an asynchronous function, please note that you must await the call to processenv:

const port = await processenv('PORT', async () => 3000);
Using the ?? operator

Instead of providing a second parameter, you may use the ?? operator to handle default values:

const isRoot = processenv('ROOT') ?? true;

Please note that this is only true if you are using the ?? operator. If you are using the old-style || operator instead, the previous line always returns true, no matter what the actual value of the ROOT environment variable is. To avoid this problem, either use the ?? operator or use the previously shown syntax using a second parameter to provide a default value.

Getting all environment variables

If you want to get all environment variables at once, omit the name and simply call processenv. The values will all be parsed, but you can not specify default values.

const environmentVariables = processenv();

Running quality assurance

To run quality assurance for this module use roboter:

$ npx roboter

Keywords

FAQs

Package last updated on 25 Mar 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc