为什么我的自定义process.env在dotenv中不起作用? [英] Why are my custom process.env not working within dotenv?

查看:1690
本文介绍了为什么我的自定义process.env在dotenv中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

得知包含API秘密密钥是不好的做法,我已经进行了一些研究,并试图学习如何创建自定义process.env.

Learning that it is a bad practice to include API secret keys I've done some research and trying to learn how to create custom process.env.

阅读后:

  • Node.js Everywhere with Environment Variables!
  • How to set NODE_ENV to production/development in OS X
  • How to set process.env from the file in NodeJS?
  • dotenv file is not loading environment variables

我正在尝试基于process.env.NODE_ENV在本地设置环境文件.该应用程序将托管在Heroku上,并在我的.gitignore中具有dev.env,但是当我尝试使用 dotenv 在本地获取undefined.我已经在终端中使用export NODE_ENV=development在本地设置了环境.当我运行命令npm startnodemon都返回undefined,但是在 env.js 中,我得到了Testing for: development,例如:

I'm trying to set an env file locally based on process.env.NODE_ENV. The application would be hosted on Heroku and in my .gitignore I have dev.env but when I try to use dotenv locally I'm getting an undefined. I have set the environment locally with export NODE_ENV=development in my terminal. When I run the command npm start or nodemon both return undefined but in env.js I get Testing for: development, example:

nodemon

[nodemon] 1.19.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node app.js`
Testing for: development
undefined

这就是我所拥有的:

app.js :

const keys = require('./config/env')
return console.log(process.env.PORT)

config/env.js :

const env = process.env.NODE_ENV
console.log(`Testing for: ${env}`)
try {
  switch(env) {
    case 'undefined':
      Error('Environment undefined, if local in terminal: export NODE_ENV=development')
      break
    case 'development':
      require('dotenv').config({
        path: './dev.env'
      })
      break
    case 'production':
      require('dotenv').config({
        path: './prod.env'
      })
      break
    default:
      Error('Unrecognized Environment')
  }  
} catch (err) {
  Error('Error trying to run file')
}

config/dev.env :

## Port number to run Application
PORT=4321

但是在 app.js 中,当我使用return console.log(process.env.PORT)return console.log(keys.PORT)进行测试时,它们都记录了undefined,为什么?使用dotenv时,我在 env.js 中似乎做错了.

but in app.js when I test with return console.log(process.env.PORT) or return console.log(keys.PORT) they both log undefined, why? I seem to be doing something wrong in env.js when using dotenv.

为了澄清,我什至没有推送到Heroku,并且 prod.env 将是验证.如果有更好的方法,请教育我.

To clarify I'm not even pushing to Heroku yet and prod.env will be validation. If there is a better approach please educate me.

推荐答案

重新阅读有关 path ,例如:

I've figured where I was going wrong after re-reading the documentation regarding path, example:

require('dotenv').config({ path: '/full/custom/path/to/your/env/vars' })

更改后:

case 'development':
  require('dotenv').config({
    path: './dev.env'
  })
  break

收件人:

case 'development':
  require('dotenv').config({
    path: `${__dirname}/dev.env`
  })
  break

有效.所以我的错误是范围问题.无需设置const keys,因此只需使用require('./config/env'),我就可以访问任何自定义进程,例如:

it works. So my error was a scope issue. No need to set const keys so just using require('./config/env') I can access any custom processes, example:

process.env.CUSTOM

或者在这种情况下为:

process.env.PORT

来自 app.js

这篇关于为什么我的自定义process.env在dotenv中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆