AlgoInsight

Q&A

How to update package.json

By AlgoInsight Team2026-06-07nodejsjavascript

Overview

package.json sits at the root of a JS project and declares its name, version, scripts, and the packages it depends on. "Updating" it can mean several things: adding/removing dependencies, bumping versions, editing scripts or metadata, or upgrading installed packages to newer releases. Using package-manager commands (rather than hand-editing) keeps package.json and the lockfile (package-lock.json / yarn.lock) consistent.

graph TD
    A[Update package.json] --> B[Add/remove deps: npm install / uninstall]
    A --> C[Edit metadata/scripts: npm pkg set / manual]
    A --> D[Upgrade versions: npm update / npm-check-updates]
    B & C & D --> E[Lockfile updated, node_modules synced]
    style A fill:#1e1b4b,color:#fff

Anatomy of package.json

{
  "name": "my-app",
  "version": "1.0.0",
  "scripts": {
    "start": "node index.js",
    "test": "jest"
  },
  "dependencies": {
    "express": "^4.18.2"
  },
  "devDependencies": {
    "jest": "^29.0.0"
  }
}

1. Adding Dependencies (auto-updates the file)

npm install express            # adds to "dependencies"
npm install --save-dev jest    # adds to "devDependencies"
npm install lodash@4.17.21     # pin a specific version

Each command writes the package and version into package.json and updates package-lock.json. (Yarn: yarn add express; pnpm: pnpm add express.)

2. Removing Dependencies

npm uninstall express          # removes from package.json + node_modules

3. Editing Fields via CLI (no manual editing)

npm pkg reads/writes fields safely:

npm pkg set version=2.0.0
npm pkg set scripts.build="webpack --mode production"
npm pkg get dependencies
npm pkg delete scripts.test

4. Bumping the Project Version

npm version patch    # 1.0.0 -> 1.0.1
npm version minor    # 1.0.0 -> 1.1.0
npm version major    # 1.0.0 -> 2.0.0

This updates the version field and creates a git commit/tag (configurable).

5. Upgrading Installed Packages

npm update                 # upgrade within the version ranges in package.json
npm outdated               # show which packages have newer versions

To upgrade beyond the declared ranges (e.g. major versions), use a tool like npm-check-updates:

npx npm-check-updates -u   # rewrites package.json to latest versions
npm install                # apply them

Understanding Version Ranges (semver)

The symbols in front of versions control how npm update upgrades:

Range Meaning Updates to
^4.18.2 Compatible minor/patch 4.x.x (not 5.0.0)
~4.18.2 Patch only 4.18.x
4.18.2 Exact pin only 4.18.2
* / latest Any newest (risky)

Understanding ^ vs ~ is key: ^ allows minor+patch updates, ~ only patch.

Manual Editing (when appropriate)

You can edit package.json by hand for fields like scripts, engines, repository, or description. After manually changing dependency versions, run npm install so the lockfile and node_modules catch up.

Why This Matters in Interviews

Node/JS interviews expect fluency with package.json: the difference between dependencies and devDependencies, semver ranges (^ vs ~), how installing/uninstalling auto-updates the manifest and lockfile, and how to upgrade safely. The role of the lockfile (reproducible installs) is a frequent follow-up.

Common Pitfalls

References

More answers