Reputation: 13
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)
})
Upvotes: 1
Views: 2473
Reputation: 8135
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
Check: https://github.com/toddbluhm/env-cmd
Upvotes: 1
Reputation: 1568
Try to use dotenv
npm
import * as dotenv from 'dotenv'
dotenv.config();
export class Constants {
process.env.<YOUR_VARIABLE_NAME>
}
Upvotes: 1