简体   繁体   中英

.env for prod and developpment with nodejs

I saw lot of different ways, some looked normal some others looked a bit more patchworked.

Can we use package json script to chose our env variables? What is the right way to do it with nodeJS and how to do it?

I have already made an .env . It contains api keys which are global for dev and prod. But I have some variables, the URL variable for exemple, which won't be the same depending on dev or prod.

Here are my scripts in the package.json

      ...
      "scripts": {
        "dev": "nodemon app.js",
        "prod": "node app.js"
      }
  1. Use cross-env package to define a NODE_ENV for the command you are running. eg "prod": "cross-env NODE_ENV=production node app.js"
  2. In the code, read the env file based on the NODE_ENV config. FWIW dotenv package can help with reading .env files.

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