The Wild

Migrate from Node.js to Bun: Step-by-Step Guide

· 5 min read · Mammals

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Migrating from Node.js to Bun replaces your runtime with a faster, all-in-one toolkit that bundles a package manager, test runner, and bundler. This guide walks you through the process safely, covering dependency swaps, API differences, and common pitfalls. You'll need a working Node.js project, basic terminal knowledge, and the ability to install software on your machine.

What You'll Need

  • An existing Node.js project (with package.json and node_modules)
  • Node.js installed (for reference and fallback)
  • Bun installed on your system (see Step 1)
  • A terminal or command prompt
  • Access to your project's dependency list and any native modules

Step-by-Step

  1. Install Bun. On macOS/Linux, run curl -fsSL https://bun.sh/install | bash. On Windows, use powershell -c "irm bun.sh/install.ps1 | iex". Verify with bun --version.
  2. Back up your project. Copy your project folder or commit all changes to version control. This ensures you can revert if anything breaks.
  3. Remove node_modules and lockfiles. Delete node_modules and any package-lock.json, yarn.lock, or pnpm-lock.yaml. These are Node-specific and can cause conflicts.
  4. Install dependencies with Bun. Run bun install. This creates a bun.lock file and installs packages into node_modules (Bun uses the same folder for compatibility).
  5. Check for native modules. If your project uses bcrypt, sharp, canvas, or similar native addons, verify they support Bun. Many do via N-API, but some may need a different version or a pure-JS alternative. Test each one individually.
  6. Update your start scripts. In package.json, change "start": "node server.js" to "start": "bun server.js" (or bun run server.js). Also update any "dev" or "test" scripts to use bun instead of node or nodemon.
  7. Replace Node-specific globals. Bun supports most Node globals like process and Buffer, but some are different. For example, __dirname and __filename work in CommonJS but not in ESM. Use import.meta.dir and import.meta.file instead. Also, require works in Bun, but ESM is preferred.
  8. Adjust for ESM/CJS differences. If your project uses "type": "module", Bun handles ESM natively. If you use CommonJS, it still works, but you may need to add .js extensions to relative imports in ESM mode. Test your imports carefully.
  9. Run your test suite. Use bun test instead of jest or mocha. Bun's test runner is Jest-compatible for most cases, but you may need to adjust mocks or setup files. If you have a custom Jest config, create a bunfig.toml to map test settings.
  10. Test your server and APIs. Start your app with bun run start and hit all endpoints. Watch for differences in error handling, streaming, or WebSocket behavior. Bun's HTTP server is based on a different engine, so performance may vary.
  11. Check environment variables. Bun automatically loads .env files, but it does not load .env.local by default. If you rely on that, add --env-file=.env.local to your start script or use Bun.env to access variables.
  12. Remove Node-specific code. Replace node:crypto with Bun's native Bun.password for hashing, or use crypto from WebCrypto. For file system, fs/promises works, but Bun.file offers faster reads. Update any code that uses child_process to spawn Node binaries—use Bun.spawn instead.
  13. Deploy and monitor. Once everything works locally, deploy to your hosting environment. Ensure your platform supports Bun (most modern ones do). Monitor logs for any runtime errors that didn't appear in Node.

Common Mistakes

  • Forgetting to delete old lockfiles—this causes Bun to use stale dependency resolutions.
  • Assuming all npm packages work flawlessly. Some packages rely on Node-specific APIs that Bun emulates but not perfectly.
  • Using __dirname in ESM without realizing it's undefined. Always use import.meta.dir.
  • Ignoring the node: prefix. Bun supports it, but some built-ins like node:worker_threads have limited support.
  • Not updating test scripts—running jest under Bun may cause conflicts.
  • Expecting identical performance metrics. Bun is faster in many cases, but not all—benchmark your own workload.

Tips

  • Use bun run for any script—it's faster than npm run.
  • Take advantage of Bun's built-in bundler by using bun build ./index.ts --outdir=dist for production builds.
  • For TypeScript projects, Bun runs .ts files directly—no need for ts-node or compilation.
  • Use bun add instead of npm install for new dependencies; it's faster and uses the same package registry.
  • If you need Node compatibility, set "bun": { "node": true } in package.json to enable more Node shims.
  • Migrate incrementally: start with a small service, then move larger apps once you're comfortable.

Troubleshooting

App won't start after migration. Check the error message. If it mentions a missing module, run bun install again. If it's a syntax error, your code may use Node-specific features that Bun doesn't support—search for the feature and replace it.

Native module fails to load. Try reinstalling with bun install --backend=copyfile (if on Windows) or rebuild with bun pm rebuild. If that fails, look for a pure-JS alternative or use a child process to call Node for that specific module.

Tests fail under bun test. Many Jest APIs work, but some like jest.mock may need adjustments. Use Bun.mock or rewrite mocks as simple functions. Check if your test setup file uses Node globals that Bun doesn't provide.

Environment variables missing. Bun reads .env automatically, but not .env.local. Use --env-file flag or rename your file to .env. Also, process.env works, but Bun.env is faster.

WebSocket or streaming issues. Bun's WebSocket implementation is different. Check the Bun docs for the correct API. For streams, use Bun.file and Bun.write instead of fs.createReadStream.

Memory leaks or crashes. Run with --smol flag to reduce memory usage, or use --expose-gc if available. If crashes persist, isolate the code causing it and test on a minimal repro.

Frequently Asked Questions

Can I run Node and Bun in the same project?

Yes, but it's messy. You can keep both runtimes and use node for specific scripts and bun for others, but you'll have two lockfiles. Better to fully migrate or use a container per runtime.

Will all npm packages work with Bun?

Most pure-JS packages work. Packages with native bindings may need updates or alternatives. Check the package's README for Bun support.

Is Bun a drop-in replacement for Node?

Not entirely. Bun aims for compatibility, but some Node APIs are incomplete (e.g., cluster, worker_threads). For most web servers and CLIs, it works well.

How much faster is Bun compared to Node?

Bun is often 2-3x faster for startup and many I/O operations, but it varies by workload. Benchmark your own app to see the real difference.

Do I need to change my code from CommonJS to ESM?

No, Bun supports both. But if you're starting fresh, ESM is recommended for better compatibility with Bun's features.

Can I use Bun with Docker?

Yes, use the official oven/bun Docker image. It's based on Alpine Linux and includes Bun pre-installed.