Migrate from Node.js to Bun: Step-by-Step Guide
In this article
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
- Install Bun. On macOS/Linux, run
curl -fsSL https://bun.sh/install | bash. On Windows, usepowershell -c "irm bun.sh/install.ps1 | iex". Verify withbun --version. - Back up your project. Copy your project folder or commit all changes to version control. This ensures you can revert if anything breaks.
- Remove node_modules and lockfiles. Delete
node_modulesand anypackage-lock.json,yarn.lock, orpnpm-lock.yaml. These are Node-specific and can cause conflicts. - Install dependencies with Bun. Run
bun install. This creates abun.lockfile and installs packages intonode_modules(Bun uses the same folder for compatibility). - 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. - Update your start scripts. In
package.json, change"start": "node server.js"to"start": "bun server.js"(orbun run server.js). Also update any"dev"or"test"scripts to usebuninstead ofnodeornodemon. - Replace Node-specific globals. Bun supports most Node globals like
processandBuffer, but some are different. For example,__dirnameand__filenamework in CommonJS but not in ESM. Useimport.meta.dirandimport.meta.fileinstead. Also,requireworks in Bun, but ESM is preferred. - 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.jsextensions to relative imports in ESM mode. Test your imports carefully. - Run your test suite. Use
bun testinstead ofjestormocha. 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 abunfig.tomlto map test settings. - Test your server and APIs. Start your app with
bun run startand 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. - Check environment variables. Bun automatically loads
.envfiles, but it does not load.env.localby default. If you rely on that, add--env-file=.env.localto your start script or useBun.envto access variables. - Remove Node-specific code. Replace
node:cryptowith Bun's nativeBun.passwordfor hashing, or usecryptofrom WebCrypto. For file system,fs/promisesworks, butBun.fileoffers faster reads. Update any code that useschild_processto spawn Node binaries—useBun.spawninstead. - 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
__dirnamein ESM without realizing it's undefined. Always useimport.meta.dir. - Ignoring the
node:prefix. Bun supports it, but some built-ins likenode:worker_threadshave limited support. - Not updating test scripts—running
jestunder Bun may cause conflicts. - Expecting identical performance metrics. Bun is faster in many cases, but not all—benchmark your own workload.
Tips
- Use
bun runfor any script—it's faster than npm run. - Take advantage of Bun's built-in bundler by using
bun build ./index.ts --outdir=distfor production builds. - For TypeScript projects, Bun runs .ts files directly—no need for ts-node or compilation.
- Use
bun addinstead ofnpm installfor 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.