1

I am using same code for multiple project, based on environment file value it give different functionality to project.

I have added:

"scripts":{
  "abc": "npm-run-all -p watch-css start-js"
  "xyz": "npm-run-all -p watch-css start-js"
}

So I want to do like if i run npm run abc it should take .env.production.abc and if I run command npm run xyz it should take .env.production.xyz.

Is it possible?

Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142
Ujjaval
  • 447
  • 5
  • 10
  • refer https://stackoverflow.com/questions/25112510/how-to-set-environment-variables-from-within-package-json-node-js – Pranav Ramachandran Apr 15 '19 at 05:50
  • @PranavRamachandran I don't want include environment variables in command I want to add env files in command because I have multiple environment variables and i don't want to pass in command. I want pass only env file. – Ujjaval Apr 15 '19 at 06:13
  • https://www.npmjs.com/package/dotenv this might helpful for you – Harish Apr 15 '19 at 06:32
  • if you are using webpack, you need to do it by define plugin in webpack config – Pravesh Khatri Apr 15 '19 at 06:57

4 Answers4

0

try the following changes. Install dotenv in your project

"scripts":{
  "abc": "npm-run-all -p watch-css -r dotenv/config start.js dotenv_config_path=/path/to/abc/env"
  "xyz": "npm-run-all -p watch-css -r dotenv/config start.js dotenv_config_path=/path/to/xyz/env"
} 
Harish
  • 1,841
  • 1
  • 13
  • 26
0

You need to use DefinePlugin in your webpack configuration.

In your webpack.config.js

module.exports = {
    .......
    new webpack.DefinePlugin({
        'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
    })
    .......
}

Now you have NODE_ENV variable here.

you need to create same file name with respect to the value you are providing in NODE_ENV.

example:

in your package.json

"abc": "NODE_ENV=staging npm-run-all -p watch-css start-js"
"xyz": "NODE_ENV=production npm-run-all -p watch-css start-js"

Now you should have

staging.js and production.js file present.

Pravesh Khatri
  • 2,124
  • 16
  • 22
  • I know how to make staging and Production ENV file and it how to use that apply. Maybe you don't understand my question. – Ujjaval Apr 17 '19 at 16:41
0

This module does that: Custom env

Erisan Olasheni
  • 2,395
  • 17
  • 20
-1

We can use npm packages like env-cmd or react-app-env which can help us to handle these type of scenarios.

Ujjaval
  • 447
  • 5
  • 10