I'm a beginner to nodejs. I created two json files with different names and mail.host. The first one is called development.json
{
"name": "My Express App - Development",
"mail": {
"host": "dev-mail-server"
}
}
and the other one is production.json
{
"name": "My Express App - Production",
"mail": {
"host": "prod-mail-server"
}
}
after using executing this code below on my index.js, the terminal prints the same output even after using set NODE_ENV="production" in the terminal. What seems to be the problem in my code? Thank you so much!
console.log(`Application Name: ${config.get('name')}`)
console.log(`Mail Server: ${config.get('mail.host')}`)
I presume you're using the config package. I've created an example app in Windows 10 to test all this using the below file structure. I suspect you just need to omit the quotes, e.g.
SET NODE_ENV=development
when you're setting the NODE_ENV variable.
Here's the app details:
Folder Structure
project
| app.js
|
└──config
| development.json
| production.json
app.js
const config = require('config');
console.log(`Application Name: ${config.get('name')}`);
console.log(`Mail Server: ${config.get('mail.host')}`);
development.json
{ "name": "My Express App - Development", "mail": { "host": "dev-mail-server" } }
production.json
{ "name": "My Express App - Production", "mail": { "host": "prod-mail-server" } }
Command Prompt Test
If I open a command window and enter:
SET NODE_ENV=development
node app.js
I get the output:
Application Name: My Express App - Development
Mail Server: dev-mail-server
If I type:
SET NODE_ENV=production
node app.js
I get the output:
Application Name: My Express App - Production
Mail Server: prod-mail-server
Powershell Test
If I open a Powershell window and enter:
$env:NODE_ENV="development"
node app.js
I get the output:
Application Name: My Express App - Development
Mail Server: dev-mail-server
If I enter:
$env:NODE_ENV="production"
node app.js
I get the output:
Application Name: My Express App - Production
Mail Server: prod-mail-server
Likewise