12factor-dotenv 中文文档教程

发布于 8年前 浏览 19 项目主页 更新于 3年前

12factor-dotenv

npm 版本构建状态

NPM downloads

Automagically Load Dotenv Files (.env)

这个模块寻找 。 env 文件用 node-env-file 加载它们并返回一个 12factor-config

功能

  • relative to parent module
    • note: Parent module is one that requires 12factor-dotenv
  • loads .env files in an ascending relative path:
    • ./
    • ../
    • ../../

Install

npm install 12factor-dotenv --save

Config schema

这个模块使用12factor-config 用于管理配置模式,用于配置支持,在此处阅读更多内容

Usage

在单个配置文件中(遵守 12factor并统一您的配置位置),执行如下操作:

// in ./app/lib/config.js

var config = require('12factor-dotenv');
var schema = {
    DEBUG: {
        env: 'DEBUG', // the environment export var to read
        type: 'boolean', // config var type (string, integer, boolean - maybe more see 12factor-config)
        default: false
    },
    PORT: {
        env: 'PORT',
        type: 'integer',
        default: 4000
    },
    MY_CUSTOM_VAR: {
        env: 'MY_LOCAL_ENVIRONMENT_EXPORT_VAR',
        type: 'string'
    },
    NODE_ENV: {
        env: 'NODE_ENV',
        type: 'string',
        default: 'development'
    }
};

var cfg = config(schema, { debug: true, env: { overwrite: true } });

## See https://www.npmjs.com/package/node-env-file#api for more details on `env` options

console.log('info: -- PORT is', cfg.PORT);
console.log('info: -- NODE_ENV is', cfg.NODE_ENV);

console.log('debug: -- ENV Debug is', process.env.DEBUG);
console.log('debug: -- ENV Port is', process.env.PORT);

console.log('info: < Configured.');

module.exports = exports = cfg;

now var cfg = require('./path/to/app/lib /config.js'); 任何你想要统一配置的地方 - 并根据环境更新 .env(s)。

笔记!

当解析 .env 文件时,这个包还会加载原生的 process.env 变量

12factor-dotenv

npm versionBuild Status

NPM downloads

Automagically Load Dotenv Files (.env)

This module looks for .env files loads them with node-env-file and returns a 12factor-config

Features

  • relative to parent module
    • note: Parent module is one that requires 12factor-dotenv
  • loads .env files in an ascending relative path:
    • ./
    • ../
    • ../../

Install

npm install 12factor-dotenv --save

Config schema

This module uses 12factor-config to manage a config schema, for configuration support, Read More Here

Usage

in a single config file (obey 12factor and unify your config location), do something like:

// in ./app/lib/config.js

var config = require('12factor-dotenv');
var schema = {
    DEBUG: {
        env: 'DEBUG', // the environment export var to read
        type: 'boolean', // config var type (string, integer, boolean - maybe more see 12factor-config)
        default: false
    },
    PORT: {
        env: 'PORT',
        type: 'integer',
        default: 4000
    },
    MY_CUSTOM_VAR: {
        env: 'MY_LOCAL_ENVIRONMENT_EXPORT_VAR',
        type: 'string'
    },
    NODE_ENV: {
        env: 'NODE_ENV',
        type: 'string',
        default: 'development'
    }
};

var cfg = config(schema, { debug: true, env: { overwrite: true } });

## See https://www.npmjs.com/package/node-env-file#api for more details on `env` options

console.log('info: -- PORT is', cfg.PORT);
console.log('info: -- NODE_ENV is', cfg.NODE_ENV);

console.log('debug: -- ENV Debug is', process.env.DEBUG);
console.log('debug: -- ENV Port is', process.env.PORT);

console.log('info: < Configured.');

module.exports = exports = cfg;

now var cfg = require('./path/to/app/lib/config.js'); wherever you want your unified config - and keep .env(s) updated per environment.

Note!

This package also loads native process.env variables, when parsing .env files

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文