npcp v0.1.0-2
npcp
An npm package config parser.
If you use Node.js you already know that npm
is awesome, but I am really sure that you mostly use it only npm install
your dependencies. Turns out that npm
has way more power than that, like creating new modules, bundling them, publishing them and it even allows you to start up your apps, set configuration options on your package.json
and even overwrite your default settins using npm config
.
Getting Started
Install the module with: npm install npcp
// without npcp
http.createServer(...).listen(process.env.npm_package_config_port);
// with npcp
var config = require('npcp');
http.createServer(...).listen(config.port);
Documentation
The usage of npcp
is really straight forward. First, you'll need to add a config
field on your package.json
, then, you'll need to create a start
(or similar) setting on the same package.json
in order to let npm inject the npm_package_config_*
variables on process.env
, and finally just assign the module to your favorite variable name and use it.
Examples
package.json
{
"name": "test",
"description": "a cool test of npcp",
"version": "0.1.0",
"scripts": {
"tryme": "node tryme.js"
},
"dependencies": {
"npcp": "0.1.0"
},
"config": {
"port": 3000,
"connections": [{
"host": "localhost:3001"
}, {
"host": "localhost:3002"
}]
}
}
tryme.js
console.log('Without npcp... :-(');
console.log('process.env.npm_package_config_port', process.env.npm_package_config_port);
console.log('process.env.npm_package_config_connections_0_host', process.env.npm_package_config_connections_0_host);
console.log('process.env.npm_package_config_connections_1_host', process.env.npm_package_config_connections_1_host);
console.log('---');
var config = require('npcp');
console.log('With npcp... :-D');
console.log('config.port', config.port);
config.connections.forEach(function(connection, index){
console.log('config.connections[' + index + ']', connection.host);
});
And, when running the above examples using npm...
$ npm run-script tryme
> npcp@0.1.0 tryme
> node tryme.js
Without npcp... :-(
process.env.npm_package_config_port 3000
process.env.npm_package_config_connections_0_host localhost:3001
process.env.npm_package_config_connections_1_host localhost:3002
---
With npcp... :-D
config.port 3000
config.connections[0] localhost:3001
config.connections[1] localhost:3002
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.
Release History
- 0.1.0-2 Updating package keywords and README.md release history.
- 0.1.0-1 Added tryme.js example.
- 0.1.0 Initial release.
License
Copyright (c) 2013 Erick Ruiz de Chavez Licensed under the MIT license.