简体   繁体   中英

Why are my process.env variables undefined?

even though I am using: require('dotenv').config() at the top of my modules, my process.env variables (set in my.env file) are undefined.

This, despite the fact that my IDE clearly is seeing them as defined bc it autofills the variable names as I type them out.

Very strange. Any thoughts?

 require('dotenv').config(); let config = { userName: process.env.userName, password: process.env.password, server: process.env.server }

here's my.env file:

    userName='myNmae'
    password='myPassword'
    server='myServer.com'
    PORT=3001

For me they were undefined for 2 reasons:

  • My variables did not have VUE_APP_ prefixed to them, for Vue that seems to be necessary.
  • I had inserted the variables into my.env file AFTER starting the server and had not restarted it yet.

Hope this helps some lost soul.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM