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,520 points
23,952 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,840 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,840 points
1,609 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,840 points
1,550 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,840 points
30,249 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,840 points
41,334 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,840 points
908 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,840 points
9,794 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,840 points
1,486 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,840 points
7,419 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,840 points
778 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 30, 2020 in Node-js by Niroj
• 82,840 points
5,770 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP