简体   繁体   English

使用回退将环境变量存储在配置文件中

[英]Storing environment variables in a config file with fallback

At the moment I am using my environment variables as is, ie, using process.env.NODE_ENV across my application, this works, but is becoming hard to keep track of, and I would therefore like to have all of these defined in a single file, ideally with fallbacks if the environment is not defined. 目前,我按原样使用我的环境变量,即在整个应用程序中使用process.env.NODE_ENV ,虽然可以正常工作,但变得难以跟踪,因此我希望将所有这些变量都定义为一个文件,如果没有定义环境,理想情况下具有回退功能。

I tried creating a config.js file like 我尝试创建一个config.js文件,例如

  export default ENVIROMENTS = {
  NODE: process.env.NODE_ENV || 'development'
  /* ... */
  }

and import it where needed, ie 并在需要的地方导入,即

`import { ENVIROMENTS.NODE } from './config.js'`

But had no luck, with errors saying ENVIROMENTS.NODE is not a function 但是运气不好,错误提示ENVIROMENTS.NODE不是一个函数

You should not name your default export. 您不应命名默认导出。

export default {
  NODE: process.env.NODE_ENV || 'development'
  /* ... */
}

and

import { NODE } from './config.js'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在节点配置文件中使用环境变量? - How to use environment variables in a node config file? angular2-starter配置文件(带有环境(测试,生产)变量,也称为.env或.conf) - angular2-starter config file (with environment (testing, production) variables aka .env or .conf) 科尔多瓦:在config.xml中使用环境变量 - Cordova: Use environment variables in config.xml 使用环境变量/参数化config.xml - Using environment variables / parameterizing config.xml 如何在 Nuxt Config 中使用 Firebase 环境变量 - How to use Firebase Environment Variables in Nuxt Config Config.get() 未从文件获取配置:“custom-environment-variables.json”Nodejs、JavaScript - Config.get() Not Getting Configuration From File: "custom-environment-variables.json" Nodejs, JavaScript babel.config.js 中的环境变量 - environment variables in babel.config.js 将 Azure 存储连接字符串放在使用配置文件而不是环境变量的 Node 应用程序中的什么位置? - Where to put the Azure Storage connection string in a Node app that uses a config file rather than environment variables? dotenv 文件未加载环境变量 - dotenv file is not loading environment variables 我们如何在 react.js 中正确使用环境变量和 env.config.js 文件(我重新加载后未定义) - how can we use environment variables properly with env.config.js file in react.js(i got undefined after reload)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM