others-How to set environment variable when using 'npm' command in node.js applications?

1. Purpose

In this post, I would demonstrate how to set environment variable to npm command, Just as the solution in my previous post, I need to execute the following command to export an envrionement variable before executing npm start:

export NODE_OPTIONS=--openssl-legacy-provider

Then I run:

npm start

But it’s too tedious to execute the export command everytime I start the application, Just as follows:

export NODE_OPTIONS=--openssl-legacy-provider
npm start

....
export NODE_OPTIONS=--openssl-legacy-provider
npm start

....
export NODE_OPTIONS=--openssl-legacy-provider
npm start

Can I do it just once?

Yes , you can, by using cross-env in your package.json.

2. The solution

2.1 The answer

TL;DR , The answer to this question is just add some characters to package.json.

Before:

"start": "react-scripts start",

After changed:

"start": "cross-env NODE_OPTIONS=--openssl-legacy-provider react-scripts start",

The key difference is that we add a prefix to the original command:

cross-env NODE_OPTIONS=--openssl-legacy-provider

Then start the application:

➜  my-app git:(master) ✗ npm start

> [email protected] start
> cross-env NODE_OPTIONS=--openssl-legacy-provider react-scripts start

Compiled successfully!

You can now view my-app in the browser.

  Local:            http://localhost:3000

Note that the development build is not optimized.
To create a production build, use npm run build.

You found that you don’t need to set the NODE_OPTIONS environment variable every time you start the app, you just need to set it to your package.json, then everything is done.

2.2 The theory

2.2.1 What is cross-env?

The core module to solve this problem is to use cross-env to export the NODE_OPTIONS, but what is cross-env?

cross-env makes it so you can have a single command without worrying about setting or using the environment variable properly for the platform. Just set it like you would if it’s running on a POSIX system, and cross-env will take care of setting it properly.

2.2.2 How to install cross-env?

You can just install cross-env using the following command:

npm install -g cross-env

Version 7 of cross-env only supports Node.js 10 and higher, to use it on Node.js 8 or lower install version 6

npm install -g cross-env@6

2.2.3 How to use cross-env?

Just as follows:

{
  "scripts": {
    "build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"
  }
}

Or you can set multiple environment variables as follows:

{
  "scripts": {
    "start": "cross-env FIRST_ENV=one SECOND_ENV=two node ./my-program"
  }
}

You can also split a command into several lines, or separate the environment variables declaration from the actual command execution. You can do it this way:

{
  "scripts": {
    "parentScript": "cross-env FIRST_ENV=\"one\" npm run childScript",
    "childScript": "cross-env-shell \"echo Hello $FIRST_ENV\""
  }
}

3. Summary

In this post, I demonstrated how to set environment variables to your node.js applications, the key solution is use cross-env tool, it can set environment variables for you in various platforms. That’s it, thanks for your reading.