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

时间:2021-08-23 23:15:04

At the moment I am using my environment variables as is, i.e., 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, i.e

并在需要的地方进口,即

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

But had no luck, with errors saying ENVIROMENTS.NODE is not a function

但没有运气,错误说ENVIROMENTS.NODE不是一个功能

1 个解决方案

#1


0  

You should not name your default export.

您不应该命名默认导出。

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

and

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

#1


0  

You should not name your default export.

您不应该命名默认导出。

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

and

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