简体   繁体   中英

How to Add variables to package.json

I'm using ReactJS, and in the package.json file, I want to change my deploy URL dynamically based on a variable. I have two different servers: "stg" and "dev" with separate deploy URL.

Something like this:

const deployServer = "dev" or "stg"

   "scripts": {
    "start": "node scripts/start.js",
    "build": "node scripts/build.js",
    "test": "node scripts/test.js",
    "deploy":"aws s3 sync build/ s3://"+deployServer+"-base-viewers-web-app"
},

How can I use that variable in the package.json file?

You can add an environment variable when the server is started:

REACT_ENV=stg npm start

Then in your package.json file you may use:

"scripts": {
  "start": "node scripts/start.js",
  "build": "node scripts/build.js",
  "test": "node scripts/test.js",
  "deploy":"aws s3 sync build/ s3://REACT_ENV-base-viewers-web-app"
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM