最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to conditionally use a development or production environment variable - Stack Overflow

programmeradmin0浏览0评论

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 module require("dotenv").config({ path: ".env" }); – Jon Leopard Commented Feb 7, 2019 at 12:47
  • dotenv only loads the variables into process.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
Add a ment  | 

2 Answers 2

Reset to default 2

From 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();
发布评论

评论列表(0)

  1. 暂无评论