I'm trying to use environment variables but when I use the file and run the server, I am getting only PORT variable and all of my other variables are undefined. I'm using nodemon.
.env
PORT=3000
SENDGRID_API_KEY=sometext
package.json
"scripts": {
"start": "node src/index.js",
"dev": "env-cmd -f ./config/dev.env nodemon src/index.js"
}
index.js
const port = process.env.PORT
const apiKey = process.env.SENDGRID_API_KEY
app.listen(port, () => {
console.log('Server is up on port '+port)
console.log(apiKey)
})
If you use -f
than it wont care about the environment -e, use one of these.
"env-cmd nodemon src/index.js"
.rc file ./.env-cmdrc
{
"development": {
"ENV1": "Thanks",
"ENV2": "For All"
},
"test": {
"ENV1": "No Thanks",
"ENV3": "!"
},
"production": {
"ENV1": "The Fish"
}
}
// Command:
env-cmd -e production nodemon index.js