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:
{
"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:
fx package.json .version1.4.2Objects such as .scripts print as JSON, and keys lists their names:
fx package.json .dependencies keys[
"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:
fx package.json '.private = false, x' savesave needs the file name as the first argument. If you pipe the input instead, fx refuses to save:
cat package.json | fx '.private = false, x' saveGoError: specify a file as the first argument to be able to save: fx file.jsonTIP
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:
fx package.json 'x.version = x.version.split(".").map((n, i) => i == 2 ? +n + 1 : n).join("."), x' .version1.4.3The trailing .version prints only the new value. Replace it with save to write the file:
fx package.json 'x.version = x.version.split(".").map((n, i) => i == 2 ? +n + 1 : n).join("."), x' saveAdd a Script to package.json
Assigning to a key in scripts adds the script, or replaces it if the key already exists:
fx package.json '.scripts.lint = "eslint .", x' .scripts{
"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
fx package.json 'delete x.dependencies.lodash, x' .dependencies{
"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:
fx package.json .dependencies 'Object.entries(x).map(([k, v]) => `${k}@${v}`)' listaxios@^1.7.2
lodash@^4.17.21
react@18.3.1
react-dom@18.3.1Find Dependencies Using a Caret Range
Merge dependencies and devDependencies, keep the entries whose version starts with ^, and print them:
fx package.json '{...x.dependencies, ...x.devDependencies}' 'Object.entries(x).filter(([k, v]) => v.startsWith("^"))' '@x.join("@")' listaxios@^1.7.2
lodash@^4.17.21
vite@^5.3.1
vitest@^1.6.0Compare Dependencies Across Workspace package.json Files
fx reads concatenated JSON one value at a time, so a glob of workspace manifests works directly:
cat packages/*/package.json | fx '`${x.name} ${x.dependencies.zod}`'@acme/api ^3.23.8
@acme/shared ^3.22.4
@acme/web ^3.23.8To find every dependency that has different versions across packages, collect all files into one array with --slurp (streaming mode):
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))'{
"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:
fx manifest.json '.permissions.push("alarms"), x' saveFor composer.json, the same commands work on .require instead of .dependencies. fx also reads tsconfig.json files that contain comments and trailing commas:
fx tsconfig.json '.compilerOptions.target = "ES2022", x'{
"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.