I'm receiving this error when I start my node application:
ReferenceError: DEV_MAIL_HOST is not defined
The following code works, when I'm specifically defining which env variable to use.
const transport = nodemailer.createTransport({
host: process.env.DEV_MAIL_HOST,
port: process.env.DEV_MAIL_PORT,
auth: {
user: process.env.DEV_MAIL_USER,
pass: process.env.DEV_MAIL_PASSWORD
}
});
However, I'm trying to conditionally inject env variables based on what mode Node starts up in.
const transport = nodemailer.createTransport({
host: process.env.NODE_ENV === "development" ? DEV_MAIL_HOST : LIVE_MAIL_HOST,
port: process.env.NODE_ENV === "development" ? DEV_MAIL_PORT : LIVE_MAIL_PORT,
auth: {
user:
process.env.NODE_ENV === "development" ? DEV_MAIL_USER : LIVE_MAIL_USER,
pass:
process.env.NODE_ENV === "development"
? DEV_MAIL_PASSWORD
: LIVE_MAIL_PASSWORD
}
});
Here's is my package.json, where I define which mode to start in.
}
"scripts": {
"start": "nodemon -e js,graphql -x NODE_ENV=production node src/index.js",
"dev": "nodemon -e js,graphql -x NODE_ENV=development node --inspect src/index.js",
}
}
What am I missing here?
Fixed thanks to @MadWard's ment. I needed to destructure my environment variables.
I'm receiving this error when I start my node application:
ReferenceError: DEV_MAIL_HOST is not defined
The following code works, when I'm specifically defining which env variable to use.
const transport = nodemailer.createTransport({
host: process.env.DEV_MAIL_HOST,
port: process.env.DEV_MAIL_PORT,
auth: {
user: process.env.DEV_MAIL_USER,
pass: process.env.DEV_MAIL_PASSWORD
}
});
However, I'm trying to conditionally inject env variables based on what mode Node starts up in.
const transport = nodemailer.createTransport({
host: process.env.NODE_ENV === "development" ? DEV_MAIL_HOST : LIVE_MAIL_HOST,
port: process.env.NODE_ENV === "development" ? DEV_MAIL_PORT : LIVE_MAIL_PORT,
auth: {
user:
process.env.NODE_ENV === "development" ? DEV_MAIL_USER : LIVE_MAIL_USER,
pass:
process.env.NODE_ENV === "development"
? DEV_MAIL_PASSWORD
: LIVE_MAIL_PASSWORD
}
});
Here's is my package.json, where I define which mode to start in.
}
"scripts": {
"start": "nodemon -e js,graphql -x NODE_ENV=production node src/index.js",
"dev": "nodemon -e js,graphql -x NODE_ENV=development node --inspect src/index.js",
}
}
What am I missing here?
Fixed thanks to @MadWard's ment. I needed to destructure my environment variables.
Share Improve this question edited Feb 7, 2019 at 13:09 Jon Leopard asked Feb 7, 2019 at 12:28 Jon LeopardJon Leopard 1,0441 gold badge13 silver badges24 bronze badges 5-
1
You're getting that error because the
DEV_MAIL_HOST
variable isn't defined in the scope of that code. Do you have a variable with that name somewhere in your code? If so, you haven't shown us. – JLRishe Commented Feb 7, 2019 at 12:34 -
1
Where are all your uppercase variables (such as
DEV_MAIL_HOST
) declared? From the code you posted, none of them is initialized or even declared as a variable, which Node seems to agree with. – Azami Commented Feb 7, 2019 at 12:34 -
DEV_MAIL_HOST
, as well as all the rest of my env variables are declared in my .env file. index.js is where I am requiring the modulerequire("dotenv").config({ path: ".env" });
– Jon Leopard Commented Feb 7, 2019 at 12:47 -
dotenv
only loads the variables intoprocess.env
, you still need to either call the variables by their name in process.env (process.env.DEV_MAIL_HOST
) or set them as constants (const DEV_MAIL_HOST = process.env.DEV_MAIL_HOST
). – Azami Commented Feb 7, 2019 at 12:49 - Yah I needed to destructure the variables. @MadWard's ment fixed the issue for me. Thank you all for your help! – Jon Leopard Commented Feb 7, 2019 at 13:08
2 Answers
Reset to default 2From both snippets and the error that is shown, you appear to be using variables without declaring them first.
Either use the variables directly by their full name (process.env.DEV_MAIL_HOST
, etc.), or initialize them at the beginning of your code:
const {
DEV_MAIL_HOST,
DEV_MAIL_PORT,
DEV_MAIL_USER,
DEV_MAIL_PASSWORD,
LIVE_MAIL_HOST,
LIVE_MAIL_PORT,
LIVE_MAIL_USER,
LIVE_MAIL_PASSWORD
} = process.env;
That's not the correct way to declare environment variables. You should declare an environment variable like: MAIL_HOST
.env file should be different for each environment. In dev .env file MAIL_HOST should contain the development URL and in production .env file MAIL_HOST should contain the production URL
You can choose the env file by using dotenv library by using
require('dotenv').config();