Skip to content

Editing package.json From the Command Line ​

Read and change package.json fields from a shell or a release script without opening an editor. fx runs a JavaScript expression against the file and can write the result back. Install fx first.

The examples below use this package.json:

json
{
  "name": "acme-web",
  "version": "1.4.2",
  "private": true,
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "test": "vitest run"
  },
  "dependencies": {
    "axios": "^1.7.2",
    "lodash": "^4.17.21",
    "react": "18.3.1",
    "react-dom": "18.3.1"
  },
  "devDependencies": {
    "typescript": "~5.4.5",
    "vite": "^5.3.1",
    "vitest": "^1.6.0"
  }
}

Read the Version From package.json ​

A path that starts with . selects a field. Strings print without quotes, so you can use them directly in scripts:

bash
fx package.json .version
1.4.2

Objects such as .scripts print as JSON, and keys lists their names:

bash
fx package.json .dependencies keys
json
[
  "axios",
  "lodash",
  "react",
  "react-dom"
]

Save Changes Back to package.json ​

fx prints the result to stdout. To write the result back to the input file, add the save function as the last argument. The pattern for any edit is: change a field, return x with the comma operator, then add save:

bash
fx package.json '.private = false, x' save

save needs the file name as the first argument. If you pipe the input instead, fx refuses to save:

bash
cat package.json | fx '.private = false, x' save
GoError: specify a file as the first argument to be able to save: fx file.json

TIP

Run the command without save first to preview the result, then run it again with save. Check the result with git diff package.json before you commit.

Bump the Patch Version in package.json ​

Split the version on ., add one to the last part, and join it back:

bash
fx package.json 'x.version = x.version.split(".").map((n, i) => i == 2 ? +n + 1 : n).join("."), x' .version
1.4.3

The trailing .version prints only the new value. Replace it with save to write the file:

bash
fx package.json 'x.version = x.version.split(".").map((n, i) => i == 2 ? +n + 1 : n).join("."), x' save

Add a Script to package.json ​

Assigning to a key in scripts adds the script, or replaces it if the key already exists:

bash
fx package.json '.scripts.lint = "eslint .", x' .scripts
json
{
  "dev": "vite",
  "build": "vite build",
  "test": "vitest run",
  "lint": "eslint ."
}

Changing a dependency version works the same way: '.devDependencies.vitest = "^2.0.5", x'. Replace the trailing selector with save to write the change.

Remove a Dependency From package.json ​

bash
fx package.json 'delete x.dependencies.lodash, x' .dependencies
json
{
  "axios": "^1.7.2",
  "react": "18.3.1",
  "react-dom": "18.3.1"
}

This edits the manifest only. Run npm install afterwards to update the lockfile and node_modules.

List Dependencies as name@version ​

Turn the object into name@version strings, and print one per line with list:

bash
fx package.json .dependencies 'Object.entries(x).map(([k, v]) => `${k}@${v}`)' list
axios@^1.7.2
lodash@^4.17.21
react@18.3.1
react-dom@18.3.1

Find Dependencies Using a Caret Range ​

Merge dependencies and devDependencies, keep the entries whose version starts with ^, and print them:

bash
fx package.json '{...x.dependencies, ...x.devDependencies}' 'Object.entries(x).filter(([k, v]) => v.startsWith("^"))' '@x.join("@")' list
axios@^1.7.2
lodash@^4.17.21
vite@^5.3.1
vitest@^1.6.0

Compare Dependencies Across Workspace package.json Files ​

fx reads concatenated JSON one value at a time, so a glob of workspace manifests works directly:

bash
cat packages/*/package.json | fx '`${x.name} ${x.dependencies.zod}`'
@acme/api ^3.23.8
@acme/shared ^3.22.4
@acme/web ^3.23.8

To find every dependency that has different versions across packages, collect all files into one array with --slurp (streaming mode):

bash
cat packages/*/package.json | fx --slurp 'x => {
  const deps = {};
  for (const pkg of x)
    for (const [name, version] of Object.entries(pkg.dependencies))
      (deps[name] ??= {})[pkg.name] = version;
  return deps;
}' 'x => Object.fromEntries(Object.entries(x).filter(([k, v]) => new Set(Object.values(v)).size > 1))'
json
{
  "lodash": {
    "@acme/api": "^4.17.21",
    "@acme/web": "^4.17.15"
  },
  "zod": {
    "@acme/api": "^3.23.8",
    "@acme/shared": "^3.22.4",
    "@acme/web": "^3.23.8"
  }
}

Edit tsconfig.json, composer.json, and manifest.json ​

Every JSON config file works the same way. For example, add a permission to a browser extension's manifest.json:

bash
fx manifest.json '.permissions.push("alarms"), x' save

For composer.json, the same commands work on .require instead of .dependencies. fx also reads tsconfig.json files that contain comments and trailing commas:

bash
fx tsconfig.json '.compilerOptions.target = "ES2022", x'
json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "strict": true,
    "outDir": "dist"
  },
  "include": [ "src" ]
}

WARNING

The output is plain JSON, so the comments and trailing commas from the original file are gone. Using save on a commented tsconfig.json removes its comments.

See Also ​