Packages and scripts
Add, remove, and update packages
Change project dependencies with Bun while keeping package.json and bun.lock synchronized.
7 minute lesson
Let’s add Zod to validate the JSON sent to our notes API:
bun add zod
Bun installs the package, adds it to dependencies, and updates bun.lock.
Development tools belong in devDependencies. We already installed TypeScript this way:
bun add --dev typescript
You can install a specific version when the project requires it:
bun add [email protected]
Remove a package
Use the package name to remove it:
bun remove zod
Bun removes the entry from package.json, updates the lockfile, and removes the installed package when nothing else needs it.
Add Zod again before continuing:
bun add zod
Update dependencies
Update packages within the version ranges declared in package.json:
bun update
Update one package with:
bun update zod
Use bun update --latest carefully. It can move packages beyond their current version ranges and introduce breaking changes.
My advice is to update a small group at a time. Read the release notes, run the tests, and commit the matching package.json and bun.lock changes together.
Lesson completed