Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use environment variables in package.json

Because we don't want sensitive data in the project code, including the package.json file, using environment variables would be a logical choice in my opinion.

Example package.json:

  "dependencies": {     "accounting": "~0.4.0",     "async": "~1.4.2",     "my-private-module":"git+https://${BB_USER}:${BB_PASS}@bitbucket.org/foo/bar.git" 

Is this possible?

The question is not if this is wise or not good, just if it's possible.

like image 409
kaasdude Avatar asked Jan 07 '16 08:01

kaasdude


People also ask

Can I use environment variables in package json?

You can use environment values to inject in your package. json like this: Any environment variables that start with npm_config_ will be interpreted as a configuration parameter. For example, putting npm_config_foo=bar in your environment will set the foo configuration parameter to bar.

What is cross env in package json?

js. method 4: Use cross-env package. This package allows environment variables to be set in one way for every platform. After installing it with npm, you can just add it to your deployment script in package.json as follows: "build:deploy": "cross-env NODE_ENV=production webpack"

How do you use environment variables?

In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable. Click Edit to modify an existing environment variable.


1 Answers

In case you use .env file, let's use grep or eval to get a value environment variable from the .env file.

Updated start2 as @Paul suggested:

"scripts": {     "start": "NODE_ENV=$(grep NODE_ENV .env | cut -d '=' -f2) some_script",     "start2": "eval $(grep '^NODE_ENV' .env) && some_script" } 
like image 96
Long Nguyen Avatar answered Sep 24 '22 20:09

Long Nguyen