Node.Js - How to Set Environment Variables in Code

Node.js - how to set environment variables in code

You can not only consume environment variables in node with process.env but also set them. This will set the variable within your current node process and any child processes it calls, but not the calling shell itself.

// consume
var alreadySetEnvVarForDevice = process.env.NOBLE_HCI_DEVICE_ID

// set
process.env['NOBLE_HCI_DEVICE_ID'] = 1

How to set environment variables from within package.json?

Set the environment variable in the script command:

...
"scripts": {
"start": "node app.js",
"test": "NODE_ENV=test mocha --reporter spec"
},
...

Then use process.env.NODE_ENV in your app.

Note: This is for Mac & Linux only. For Windows refer to the comments.

Setting Environment Variables for Node to retrieve

Environment variables (in this case) are being used to pass credentials to your application. USER_ID and USER_KEY can both be accessed from process.env.USER_ID and process.env.USER_KEY respectively. You don't need to edit them, just access their contents.

It looks like they are simply giving you the choice between loading your USER_ID and USER_KEY from either process.env or some specificed file on disk.

Now, the magic happens when you run the application.

USER_ID=239482 USER_KEY=foobar node app.js

That will pass the user id 239482 and the user key as foobar. This is suitable for testing, however for production, you will probably be configuring some bash scripts to export variables.

Set environment variable through Typescript script

Can't (easily) change environment variables for parent process

You can change/set the environment for the currently running process. That means that when ts-node runs your program, you are changing the environment variables for your script and for ts-node.

After your script is finished running, ts-node stops, and the environment changes are lost. They don't get passed back to the shell.

Changing another process's environment

Changing the environment variables for the parent process (the shell) is a much more complicated process and depends on your OS and upon having the correct permissions. For linux, one such technique is listed here. In Windows, you can find some hints by looking at this question.

Other options

Your other option might be to just return a string that your shell understands, and run that.

How can I set environment variables in my Javascript file rather than the command line?

The package dotenv will pull from a .env file in your project.

https://www.npmjs.com/package/dotenv

Use node.js commandline argument as environment variable

When doing node index.js a=5, a=5 is an argument for node, as index.js is.

If you want to pass an environment variable, you must specify it before node command : a=5 node index.js.

The node process.env is populated with your bash environment variables.

How can I set an environmental variable in node.js?

You can set your environment variables in process.env:

process.env['VARIABLE'] = 'value';

-OR-

process.env.VARIABLE = 'value';

Node should take care of the platform specifics.

How can I set NODE_ENV=production on Windows?

Current versions of Windows use Powershell as the default shell, so use:

$env:NODE_ENV="production"

Per @jsalonen's answer below. If you're in CMD (which is no longer maintained), use

set NODE_ENV=production

This should be executed in the command prompt where you intend to run your Node.js application.

The above line would set the environment variable NODE_ENV for the command prompt where you execute the command.

To set environment variables globally so they persist beyond just the single command prompt, you can find the tool from System in Control Panel (or by typing 'environment' into the search box in the start menu).



Related Topics



Leave a reply



Submit