Adds a key and value to the closest package.json

Usage no npm install needed!

<script type="module">
  import pkgAdd from '';



Adds a key and value to the closest package.json

Build Status npm version Dependency Status js-standard-style


Install with npm

$ npm i -g pkg-add


pkg-add is primarily a command line utility for one aspect of managing package.json files

$ echo '{"foo": "bar"}' | pkg-add > package.json


pkg-add simply adds field into the closest package.json and will fail if the key already exists.

To use simply stream in a JSON object to merge with the package json.

-f --force

Use -f to force overwriting existing keys in the package json.

Programmatic Usage

$ npm i -S pkg-add

The main pkg-add function needs an object which acts as a master, usually this is from the package.json, but, it needn’t be, any object will do. The object is treated as immutable, fresh objects will always be returned.

The only current option is whether to force merging over the master object, this defaults to false and will throw an error if keys exist on the master.

The main function returns another function which can be used to add properties. This add function simply merges the objects and returns a new one.

const package = require('package.json')
const add = require('pkg-add')(pkg, {
  force: false  

console.log(add('foo', 'bar'))
// or
  foo: 'bar'

Running tests

$ npm install
$ npm test


Pull requests are always welcome, please follow the standard style and ensure all tests are passing.

For bugs and feature requests, please create an issue.