是否可以从节点包的`bin`命令访问npmconfiguration设置?

我想添加一些默认configuration到shell工具,用户可以使用npm config来覆盖。 它看起来像这是可能的,但这只适用于npm script 不是我的包的二进制文件,由"bin"下面指定。

的package.json

 { "name": "tmp", "scripts": { "test": "node index.js" }, "bin" : { "tmp" : "index.js" }, "config": { "foo" : 123 } } 

index.js

 #!/usr/bin/env node console.log(process.env.npm_package_config_foo); 

我的企图

调用二进制

为此设置是在包中运行npm link以创build该包的全局链接

 $ tmp undefined 

将脚本传递给节点

 $ node ./index.js undefined 

调用npm script只有这个工程

 $ npm test > tmp@ test /private/tmp > node index.js 123 

我怎样才能访问这些configuration值的方式可以覆盖像npm config允许的用户 ? (为了清楚我想用npm config方式来做,我知道还有其他的方法去处理这个特定的猫。)

您可以在https://github.com/kevva/npm-conf中使用名为npm-conf的包。

首先你需要安装它:

 npm install --save npm-conf 

用法如下:

 const npmConf = require('npm-conf'); const conf = npmConf(); conf.get('prefix') //=> //=> /Users/unicorn/.npm-packages conf.get('registry') //=> https://registry.npmjs.org/ 

如果你想获得所有可用的NPMconfiguration选项列表:

 npm config list -l