12factor-config

中文文档
2.0.0发布于 2018/02/06MIT零依赖近 30 天下载 3079

Read the config for your app from only the environment.

原始使用文档
文档目录6 节
  1. Stability
  2. Synopsis
  3. Valid Values
  4. What I Do
  5. Author
  6. License

12factor-config

A config module which only reads the environment. For Node.js that means process.env.

Note 1 : We will not ever use NODE_ENV in any example here since each environment should specify everything it needs and nothing should be dependent on being a particular environment (such as 'development', 'testing', 'staging' or 'production').

Note 2 : This package is considered stable so the fact that there are few commits these days is no sign of inactivity, just stability. Please use and submit PRs for bugs. New features will be considered too.

Stability

Please note that this project is pretty much feature complete. Issues will be fixed. New features may be added if necessary. Otherwise it won't have very many more releases - but rest assured I will support it and you. The project is not dead, it is merely resting.

Synopsis

Firstly, set some environment variables that your program will look for. You don't need to set any that have a 'default' but you do need to set any that are 'required'.

$ export REDIS_URL=redis://user:password@hostname:port/db
$ export APPNAME_PORT=8080

Then, in your program:

var config = require('12factor-config');

var cfg = config({
    redisUrl : {
        env      : 'REDIS_URL',
        type     : 'string', // default
        required : true,
    },
    logfile : {
        env      : 'APPNAME_LOG_FILE',
        type     : 'string',
        default  : '/var/log/appname.log',
        required : true,
    },
    port : {
        env      : 'APPNAME_PORT',
        type     : 'integer',
        default  : '8000',
        required : true,
    },
    debug : {
        env      : 'APPNAME_DEBUG',
        type     : 'boolean',
        default  : false,
    },
    env : {
        // you really shouldn't use this, but some people/packages do
        env      : 'NODE_ENV',
        type     : 'enum',
        values   : [ 'development', 'test', 'stage', 'production', ],
    },
});

console.log(cfg);

Should output something like:

{
  redisUrl: 'redis://user:password@hostname:port/db',
  logfile: '/var/log/appname.log',
  port: 8080,
  debug: false,
  env: 'development'
}

It is advisable to prefix your environment variables with a prefix related to your application name as shown in the later config vars above. Mainly this is to namespace your vars and not stomp over others already defined. Of course you don't need to use the prefix in the local name.

Valid Values

When you have certain types, some transformations are performed to get it from a string (since the environment only contains string) into the required value.

  • boolean - upper or lower case is allowed
    • true - 'true', 't', 'yes', 'y', 'on', '1'
    • false - 'false', 'f', 'no', 'n', 'off', '0'
    • any other value throws

What I Do

I usually have a lib/cfg.js such as the following:

var config = require('12factor-config');

var cfg = config({
    // ... environment config here ...
});

module.exports = cfg;

By doing this, all other files in your application can just require('lib/cfg.js') and obtain the exact same configuration.

Author

Written by Andrew Chilton - Twitter.

License

MIT - http://chilts.mit-license.org/2013/

(Ends)

版本与发布记录

12factor-config 共发布 9 个版本,首次发布于 2014/01/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.0latest2018/02/068 年前
1.3.12016/10/2110 年前
1.3.02016/10/0710 年前
1.2.12016/07/1910 年前
1.2.02016/07/1910 年前
1.1.02015/12/1110 年前

关于 12factor-config 的常见问题

12factor-config 是什么?

Read the config for your app from only the environment.

如何安装 12factor-config?

在项目目录运行 npm install 12factor-config,或使用 pnpm add / yarn add / bun add。

12factor-config 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/12factor-config 是否存在。

12factor-config 可以商用吗?

12factor-config 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

12factor-config 的下载量有多少?

根据 npm 官方下载统计,12factor-config 在 2026/10/04 – 2026/11/02 这 30 天内被下载 3,079 次,日均约 103 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

12factor-config 还在维护吗?

最新版本 2.0.0 发布于 2018/02/06(8 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2014/01/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。