Environment variables will be injected when Node server starts, that allows us to use different values for development and production. Typicall we use this for login, passwords, etc…

app.js

const MONGODB_URI = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@cluster0-ntrpwp.mongodb.net/${process.env.MONGO_DEFAULT_DATABASE}`;
...
app.listen(process.env.PORT || 3000);



With nodemon we can provide configuration file nodemon.json.

nodemon.json

{
    "env": {
        "MONGO_USER": "michal",
        "MONGO_PASSWORD": "password",
        "MONGO_DEFAULT_DATABASE": "shop"
    }
}



But in production we will not use nodemon because we will be not restarting server after every change and also we will not be doing any changes in production. So we can update package.json file.

package.json

...
"scripts": {
    "start": "NODE_ENV=production MONGO_USER=michal MONGO_PASSWORD=password MONGO_DEFAULT_DATABASE=shop node app.js",
    "start-server": "node app.js",
    "start:dev": "nodemon app.js"
}



We can then start our app locally with npm run start:dev.