Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set environment variables in a cross-platform way?

For Windows, my Node scripts should look like this:

"scripts": {
    "start-docs": "SET NODE_ENV=development&&babel-node ./docs/Server.js"
}

But on Linux there's no SET, so it would be like this:

"scripts": {
    "start-docs": "NODE_ENV=development&&babel-node ./docs/Server.js"
}

Is there a way to declare environment variables in a way that is consistent and cross-platform?

like image 327
André Pena Avatar asked Sep 07 '15 22:09

André Pena


People also ask

How do I permanently set environment variables?

To make permanent changes to the environment variables for all new accounts, go to your /etc/skel files, such as . bashrc , and change the ones that are already there or enter the new ones. When you create new users, these /etc/skel files will be copied to the new user's home directory.


2 Answers

I recently came across the cross-env project. It's pretty straight-forward

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

That will set the build environment variable to production regardless of the OS.

like image 196
André Pena Avatar answered Oct 08 '22 19:10

André Pena


I would vote against setting this in package.json because environment variables should be set dependent on your environment while package.json is most likely the same for every environment (you commit it to your version control system, right?). Instead you should use something like dotenv if you are looking for a clean and generic solution.

like image 29
DanielKhan Avatar answered Oct 08 '22 19:10

DanielKhan