Edit package.json from command line

Jsonnode.jsNpm

Json Problem Overview


I'm trying to add or edit a variable in my package.json from a shell script. So if i have a package.json like this:

{
  "name": "my-project",
  "description": "Project by @DerZyklop",
  "version": "0.0.0",
  ...

I want a command like

npm config set foo bar

that adds a new field like

{
  "name": "my-project",
  "description": "Project by @DerZyklop",
  "foo": "bar",
  "version": "0.0.0",
  ...

...but unfortunately npm config set just edits the ~/.npmrc and not my package.json.

Json Solutions


Solution 1 - Json

The package.json is just a json file, so you could use the tool json. To install it use:

npm install -g json

Then you can edit a file in-place. More information here.

Example

$ cat package.json
{
  "name": "my-project",
  "description": "Project by @DerZyklop",
  "version": "0.0.0"
}

$ json -I -f package.json -e "this.foo=\"bar\""
json: updated "package.json" in-place

$ cat package.json
{
  "name": "my-project",
  "description": "Project by @DerZyklop",
  "version": "0.0.0",
  "foo": "bar"
}

Solution 2 - Json

If you don't want to install sponge or json, you can also do

echo "`jq '.foo="bar"' package.json`" > package.json

Solution 3 - Json

If you don't want to install anything, you can also use a one-line script to modify the package.json:

node -e "let pkg=require('./package.json'); pkg.homepage='${CI_PAGES_URL}'; require('fs').writeFileSync('package.json', JSON.stringify(pkg, null, 2));"

Solution 4 - Json

You can also use [tag:jq] and sponge (moreutils package) like this :

jq '.foo="bar"' package.json | sponge package.json

With an environment variable :

jq --arg h "$HOMEPAGE" '.homepage=$h' package.json | sponge package.json

Solution 5 - Json

I wanted to update only the version property in package.json and this is what worked for me:

# this sets the version 
# in package.json to 1.0.2
npm version 1.0.2 

Solution 6 - Json

There's also a npm package for doing this called npe: https://github.com/zeke/npe

cd some/node/project

# Get stuff from package.json
npe name
npe scripts
npe scripts.test
npe repository.url
open $(npe repository.url)

# Set stuff in package.json
npe name foo
npe scripts.start "node index.js"

# Keywords string will be turned into an array
# If commas are present, they'll be the delimiter. Otherwise spaces.
npe keywords "foo, bar, cheese whiz"
npe keywords "foo bar baz"

# The current working directory's package.json is used by default,
# but you can point to another package file with a flag:
npe name --package=some/other/package.json
npe name other --package=some/other/package.json

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDerZyklopView Question on Stackoverflow
Solution 1 - Jsonenrico.bacisView Answer on Stackoverflow
Solution 2 - JsonAmy GuoView Answer on Stackoverflow
Solution 3 - JsonralfstxView Answer on Stackoverflow
Solution 4 - JsonBertrand MartelView Answer on Stackoverflow
Solution 5 - JsonAakashView Answer on Stackoverflow
Solution 6 - JsonZekeView Answer on Stackoverflow