Edit package.json from command line
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));"
You can also use 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
If you don't want to install sponge or json, you can also do
echo "`jq '.foo="bar"' package.json`" > package.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"
}