How to set environment variables from within package json

0 votes

How to set some environment variables from within package.json to be used with npm start like commands?

Here's what I currently have in my package.json:

{
  ...
  "scripts": {
    "help": "tagove help",
    "start": "tagove start"
  }
  ...
}

I want to set environment variables (like NODE_ENV) in the start script while still being able to start the app with just one command, npm start.

Jul 13, 2020 in Node-js by kartik
• 37,490 points
6,493 views

1 answer to this question.

0 votes

Hello @kartik,

Set the environment variable in the script command:

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

Then use process.env.NODE_ENV in your app.

Note: env ensures that it works across platforms. You can omit it if you only care about Mac/Linux.

Hope it helps!!

Thank You!!

answered Jul 13, 2020 by Niroj
• 82,680 points

Related Questions In Node-js

0 votes
1 answer

How to read environment variables in Node.js?

Hello @kartik, Yes,you can read environment variables in Node.js ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,680 points
325 views
0 votes
1 answer

How to use executables from a package installed locally in node_modules?

Hello @kartik, Use the npm bin command to get the ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,680 points
181 views
0 votes
1 answer

How to clean node_modules folder of packages that are not in package.json?

Hello @kartik, You could remove your node_modules/ folder ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,680 points
9,063 views
0 votes
1 answer

How do I add a custom script to my package.json file that runs a javascript file?

Hello @kartik, I have created the following, and ...READ MORE

answered Jul 16, 2020 in Node-js by Niroj
• 82,680 points
4,411 views
0 votes
1 answer

How to write files in Node.js?

Hello @kartik, Currently there are three ways to ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,680 points
170 views
0 votes
1 answer

How to Install a local module using npm?

Hello @kartik, This is what worked for me: npm ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,680 points
369 views
0 votes
1 answer

How to run Gulp tasks sequentially one after the other?

Hello @kartik, By default, gulp runs tasks simultaneously, ...READ MORE

answered Jul 12, 2020 in Node-js by Niroj
• 82,680 points
237 views
0 votes
1 answer

How to npm install to a specified directory?

Hello @kartik, You can use the --prefix option: mkdir -p ./install/here/node_modules npm ...READ MORE

answered Jul 15, 2020 in Node-js by Niroj
• 82,680 points
2,295 views
0 votes
1 answer

How to set Environment Variables for Node to retrieve?

Hello, Environment variables (in this case) are being ...READ MORE

answered Nov 26, 2020 in Node-js by Niroj
• 82,680 points
46 views
0 votes
1 answer

How to create package.json from package-lock.json?

Hello @kartik, Install the latest npm with npm install ...READ MORE

answered Nov 29, 2020 in Node-js by Niroj
• 82,680 points
845 views