Create your own
Lesson illustration

Bun Package Management Basics

Welcome back. In our last lesson, we explored the architectural decisions that make Bun a compelling alternative to Node.js, focusing on its performance, native TypeScript support, and all-in-one philosophy. You learned why Bun is so fast and how it streamlines the developer workflow.

Now, we'll move from the conceptual to the practical by focusing on a cornerstone of that all-in-one toolkit: Bun's built-in package manager. As an experienced developer, you are well-versed in using tools like npm or Yarn. This lesson will show you how to translate that knowledge to Bun's ecosystem, enabling you to add and manage project dependencies with remarkable speed and efficiency. We will cover the essential commands for installing, adding, removing, and updating packages, preparing our project for the grammY framework we'll use to build our bot.

1. A Faster npm install

In the previous lesson, we discussed Bun's performance. The package manager is a prime example of this. When installing dependencies, Bun is significantly faster than its predecessors.

This benchmark illustrates the installation speed of Bun compared to pnpm, npm, and Yarn for a Remix application, highlighting Bun's performance advantage.

This speed isn't just for new projects. Bun is designed as a "drop-in replacement" for npm, meaning you can adopt it in existing Node.js projects with minimal friction. It respects existing configurations and ensures a smooth transition.

Migrate from npm install to bun install

The official Bun documentation explains this compatibility clearly.

Read the introductory section, from the title up to the features list. Pay close attention to how it automatically handles package-lock.json and respects .npmrc files, which are key to its "drop-in" nature.

2. Core Commands: An npm Developer's Rosetta Stone

Your existing knowledge of package management directly translates to Bun. The commands are intuitive and often parallel their npm counterparts, with a few minor differences in naming and flags.

The article "How to Use Bun as a Package Manager" provides a fantastic side-by-side comparison.

How to Use Bun as a Package Manager

This resource is excellent for quickly mapping your npm knowledge to Bun.

Focus on the table under the heading Bun Install vs npm/yarn. This table will be your go-to reference. Notice the slight differences, such as bun add instead of npm install <pkg> and bun remove instead of npm uninstall. The flag for dev dependencies is also -d instead of npm's -D or --save-dev.

Here is a summary of the most essential commands:

Actionnpm CommandBun Command
Install all dependenciesnpm installbun install (or bun i)
Add a production dependencynpm install <pkg>bun add <pkg>
Add a development dependencynpm install -D <pkg>bun add -d <pkg>
Remove a dependencynpm uninstall <pkg>bun remove <pkg> (or bun rm)

For a quick visual demonstration of these commands in action, the following video clips from a freeCodeCamp course are helpful.

Bun Tutorial – JavaScript Runtime (Node.js Alternative) [Full Course]

Watch these two short clips to see bun add being used to install dependencies in a TypeScript project.

First, watch the segment where figlet is installed, from this part. Then, you can see a similar process for installing express from this clip. This reinforces how straightforward the process is.

3. The Binary Lockfile: bun.lockb

When you run bun install, you'll notice a new file in your project: bun.lockb. This is Bun's lockfile, analogous to package-lock.json or yarn.lock. The key difference is that bun.lockb is a binary file, not plain text.

This binary format is one of the reasons for Bun's speed. It's much faster for the machine to parse and write compared to a large JSON or YAML file.

As with any lockfile, its purpose is to ensure deterministic and reproducible builds by locking down the exact versions of all dependencies and sub-dependencies. You should always commit bun.lockb to your version control system.

What if you're collaborating with team members who still use Yarn? Bun has you covered. You can instruct it to generate a compatible yarn.lock alongside bun.lockb.

How to Use Bun as a Package Manager

This article explains the lockfile and provides a list of best practices.

First, read the section on the lockfile format to understand its purpose and the compatibility options. Then, review the Best Practices Summary at the end. Points 2 and 3 about committing the lockfile and using --frozen-lockfile in CI/CD are standard industry practices you'll recognize.

4. Managing and Auditing Dependencies

Beyond the basics, Bun provides a suite of commands for the ongoing management of your project's dependencies, with some notable developer experience improvements.

  • bun update: Updates dependencies to the latest version that satisfies the semver range in your package.json.
  • bun outdated: Checks for dependencies that have newer versions available.
  • bun pm ls: Lists your installed packages, which is useful for quickly seeing your project's dependency tree.

Bun also includes some particularly useful interactive and diagnostic tools.

Bun 1.3 Can Now Replace Your Entire JavaScript Stack

This video highlights some of Bun's more advanced and user-friendly package management features.

Watch the short segment from the beginning of the package manager section. It demonstrates three excellent commands: bun update --interactive: An interactive UI to select which packages to update. bun outdated: A clean, table-based view of outdated packages. bun y <pkg> (or bun pm why <pkg>): A powerful command that tells you exactly why a specific package is installed in your node_modules tree, which is invaluable for debugging dependency conflicts.

Hands-on: Adding grammY to Your Project

Now, let's apply this to our Telegram bot project.

  1. Navigate to the project directory you created in the first lesson.
  2. We need to add grammY, the core framework for our bot. Run the following command:
    bun add grammy
    
  3. We'll also need the associated TypeScript types for the best development experience. Add them as a development dependency:
    bun add -d @grammyjs/types
    
  4. After these commands complete, inspect your project. You should see:
    • package.json has been updated with grammy in dependencies and @grammyjs/types in devDependencies.
    • A bun.lockb file has been created.
    • The node_modules directory now contains grammy and all its transitive dependencies.

You can now try out some of the management commands we just learned:




# See what was just installed
bun pm ls




# Verify there are no outdated packages (since you just installed them)
bun outdated

You have now successfully used Bun to manage your project's dependencies.

Conclusion

In this lesson, you've mastered the fundamentals of Bun's built-in package manager. Building on your extensive experience with npm, you now know how to perform all the essential dependency management tasks in a faster, more integrated environment.

Key Takeaways:

  • Performance and Compatibility: Bun's package manager is incredibly fast and serves as a drop-in replacement for npm, respecting existing Node.js project configurations.
  • Core Commands: The commands for managing dependencies (install, add, remove) are intuitive and map closely to their npm equivalents.
  • Binary Lockfile: bun.lockb is a key component for ensuring fast, reproducible builds. It should always be committed to version control.
  • Advanced Tooling: Bun offers superior developer experience features like interactive updates (bun update --interactive) and dependency analysis (bun y <pkg>).

With our core dependency, grammY, now installed, our project is taking shape. In the next lesson, we will explore another of Bun's core features: its ability to run and debug TypeScript files directly, further simplifying our development loop as we begin to write our first lines of bot code.

Can't find a good explanation? Sign up and we'll make it for you

Sign up