Making it easy for your npm package to save its config in the host application's package.json
.
npm install package-json-config
Some App is an application that has taken a dependency on your npm package, Greeter.
// some-app/package.json
{
"name": "some-app",
"greeterConfig": {
"recipient": "Mr. Anderson"
}
}
// some-app/index.js
const greet = require('greeter')
greet()
// some-app/node_modules/greeter/index.js
const getConfig = require('package-json-config')
const config = getConfig('greeterConfig')
module.exports = function () {
console.log(`Hello, ${config.recipient}.`) // Hello, Mr. Anderson.
}
In this repo, you can find a working example and instructions for how to run it.
You must use package-json-config
in the entry-point module of your npm package. It uses module.parent.parent
to find the module using your package so you must use package-json-config
in the entry point (index.js) of your package. If you find this makes using package-json-config
too difficult in your package, submit a new issue describing your scenario and I'll see what I can do. Pull requests are encouraged as well 😉