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

javascript - Dotenv unable to overwrite key value pair - Stack Overflow

programmeradmin4浏览0评论

I am trying to implement environment variables using dotenv on my node server, however I am having trouble loading them in from the .env file which is located in the root. When I run const dotenv = require("dotenv").config({debug: true}); I encounter the following message: "USER" is already defined in process.env and will not be overwritten

Additionally when I try to load the page, it encounter the following error: ER_DBACCESS_DENIED_ERROR: Access denied for user ''@'localhost' to database '_api'

.env:

USER=root
PASS=

I am trying to implement environment variables using dotenv on my node server, however I am having trouble loading them in from the .env file which is located in the root. When I run const dotenv = require("dotenv").config({debug: true}); I encounter the following message: "USER" is already defined in process.env and will not be overwritten

Additionally when I try to load the page, it encounter the following error: ER_DBACCESS_DENIED_ERROR: Access denied for user ''@'localhost' to database '_api'

.env:

USER=root
PASS=
Share Improve this question edited Dec 22, 2020 at 11:17 Lin Du 102k135 gold badges332 silver badges564 bronze badges asked Dec 22, 2020 at 10:53 JSON_DeruloJSON_Derulo 9924 gold badges17 silver badges45 bronze badges 2
  • How did you run the js file? Did you set the environment variable via the mand line? – Lin Du Commented Dec 22, 2020 at 11:03
  • @slideshowp2 if I manually set the environment variables in the mand line everything is fine, however I have const dotenv = require("dotenv").config({debug: true}); on the files that use the environment variables and it seems to not recognize them – JSON_Derulo Commented Dec 22, 2020 at 11:05
Add a ment  | 

2 Answers 2

Reset to default 7

From the Variables overwriting/priority.

Environment variables that are already set will not be overwritten, that means that the mand line variables have a higher priority over all those defined in env files;

And this What happens to environment variables that were already set?

We will never modify any environment variables that have already been set. In particular, if there is a variable in your .env file which collides with one that already exists in your environment, then that variable will be skipped. This behavior allows you to override all .env configurations with a machine-specific environment, although it is not remended.

If you want to override process.env you can do something like this:

const fs = require('fs')
const dotenv = require('dotenv')
const envConfig = dotenv.parse(fs.readFileSync('.env.override'))
for (const k in envConfig) {
  process.env[k] = envConfig[k]
}

As of version 16.0.1, you can override process.env values with require('dotenv').config({ override: true }).

.env:

access_token=aa

index.js:

require('dotenv').config({ override: true })
process.env.access_token = bb
console.log(process.env.access_token) //bb
发布评论

评论列表(0)

  1. 暂无评论