Do Node.js Scripts Run on Bun Without Changes?
I ran scripts written for Node.js with the bun command without changing a single character. Eight scripts that use file operations, an HTTP server and fetch, child processes, Workers, and so on all produced the same output values and exit codes as Node.js. For one run, Node.js took 30.9 ms and Bun took 5.8 ms.
However, the results were not the same in every case. Three things need to be known before you switch: .env is loaded automatically, tests written with node:test do not run under bun, and the native addon better-sqlite3 cannot be loaded. This article is meant to help you decide whether to run your own automation scripts or CLI tools faster with Bun, or to check how well a migration from Node.js to Bun will work.
The test environment was Ubuntu 24.04 (x86_64, 4 CPUs), Node.js 22.23.4 (npm 10.9.9), and Bun 1.4.4. All scripts are JavaScript (CommonJS .js and ES Modules .mjs). No TypeScript is used. I did not test Windows or macOS, frameworks such as Express or Next.js, or native addons other than better-sqlite3. For timing, I measured short scripts from startup to exit. I did not measure the speed of scripts that do heavy computation or large amounts of I/O.
- 1. Things That Produced the Same Results
- 2. Startup Is About 5 Times Faster on Bun
- 3. The Version and Executable Name Change
- 4. .env Is Loaded Without Any Options
- 5. Run node:test Tests with bun test
- 6. better-sqlite3, a Native Addon, Cannot Be Loaded
- 7. Running from package.json scripts requires –bun
- 8. Error Display and Stack Trace Format
Things That Produced the Same Results
I ran each of the following eight scripts with the same file using node 01-cjs.js and bun 01-cjs.js, then compared the output.
| Script | What it uses |
|---|---|
| 01-cjs.js | require, __dirname, require.main === module, require of JSON |
| 02-esm.mjs | Top-level await, import.meta.dirname, import ... with for JSON |
| 03-fs-path-os.mjs | Synchronous fs APIs (recursive mkdir and readdir, rename, rm), path, os |
| 04-http-fetch.mjs | http.createServer, fetch, http.get |
| 05-child-process.mjs | execFileSync, exit status from spawnSync, exec |
| 06-crypto-zlib-stream.mjs | Hashing, HMAC, scryptSync, gzip, pipeline from stream/promises |
| 07-events-timers-worker.mjs | events.once, timers/promises, worker_threads, AsyncLocalStorage |
| 08-cli.mjs | util.parseArgs, environment variables, readline on stdin, process.exitCode |
All eight scripts produced the same output values and exit codes as Node.js. For example, a script that starts an HTTP server and calls fetch on it runs on Bun like this:
$ bun 04-http-fetch.mjs
status: 200 application/json
body: {"method":"GET","url":"/hello?x=1"}
http.get body: {"method":"GET","url":"/via-http-get"}
server closed
However, the appearance of arrays and objects printed with console.log differed. When I printed the result of parseArgs in 08-cli.mjs, Node.js showed [ 'file.txt' ] and Bun showed [ "file.txt" ]. The official Bun documentation on Node.js compatibility also states that Bun formats console output in its own way, and that the way it orders objects differs from util.inspect. The parts that printed the same values with JSON.stringify ({"name":"tama","verbose":true}) gave the same strings on both runtimes. If another program reads the output of console.log, printing JSON avoids being affected by this difference.
Startup Is About 5 Times Faster on Bun
I ran 01-cjs.js 20 times in a row and measured the total time three times. Node.js took 0.617, 0.610, and 0.647 seconds. Bun took 0.117, 0.123, and 0.115 seconds. Dividing the median by 20 gives 30.9 ms per run for Node.js and 5.8 ms for Bun.

The Version and Executable Name Change
When I read process.version in Bun, it returned v26.3.0, even on an environment where Node.js 22 was installed. The Bun 1.4.4 compatibility page is also written against Node.js v26. A script that branches on the version number will be judged as Node.js 26 when it runs under Bun.
process.execPath also points to the Bun executable. Here is the output of 05-child-process.mjs, which starts a child process using process.execPath:
$ bun 05-child-process.mjs
execFileSync echo: hi
spawnSync status: 3
execPath: bun -> v26.3.0
exec node: v22.23.4
A child process started with process.execPath ran Bun, while a child process started by the name node ran the real Node.js 22. Parent and child runtimes get mixed, so a script that uses child processes needs to decide which runtime it should use and write the calls accordingly.
.env Is Loaded Without Any Options
I put a .env file containing FROM_DOTENV=hello in the same folder as the script and printed process.env.FROM_DOTENV.
$ node 13-env.mjs
FROM_DOTENV: undefined
$ bun 13-env.mjs
FROM_DOTENV: hello
$ bun --no-env-file 13-env.mjs
FROM_DOTENV: undefined
Node.js reads .env only when you pass the --env-file option. Bun reads it even without any option. According to the Bun environment variables documentation, Bun also automatically reads files such as .env.production and .env.development, depending on the value of NODE_ENV, in addition to .env.
If you run a production script with Bun in a folder that contains a development .env, the values from the development .env are used for any settings you did not pass as environment variables. If you want to pass settings only through environment variables, run the script with --no-env-file.
Run node:test Tests with bun test
When I ran a test file written with node:test directly with bun, the same way I would with node, it stopped with an error and ran no tests.
$ bun 11-node-test.mjs
error: Cannot use test outside of the test runner. Run "bun test" to run tests.
(Source lines and the stack trace are omitted.)
As the message says, running bun test ./11-node-test.mjs ran two tests. The result was one pass and one fail, the same as Node.js. The diff shown on failure also uses the same + actual - expected format as Node.js. However, the result output is in Bun's own format. Node.js outputs TAP format when the output is not a terminal (in my test I sent the output to a file, so it was TAP), and it outputs spec format when run in a terminal.
The compatibility page lists node:test as partially implemented. It says that node:test/reporters, code coverage, and ways of running such as node --test are not supported. In fact, import('node:test/reporters') gave an ERR_UNKNOWN_BUILTIN_MODULE error on Bun. If you use a custom reporter built on node:test/reporters, or rely on TAP output, you cannot replace it as is.
I imported 13 built-in modules. Only two produced errors: node:test/reporters, and node:sea, which creates single executable files. The compatibility page also lists node:sea as not implemented, and recommends using bun build --compile instead.
better-sqlite3, a Native Addon, Cannot Be Loaded
I installed better-sqlite3 12.11.1, which includes native addons (.node files), with npm install, and ran a script that opens an in-memory database. Node.js printed [ { name: 'tama', age: 3 } ]. On Bun, the script stopped at the point where the module was loaded.
$ bun t.cjs
error: 'better-sqlite3' is not yet supported in Bun.
Track the status in https://github.com/oven-sh/bun/issues/4290
In the meantime, you could try bun:sqlite which has a similar API.
code: "ERR_DLOPEN_FAILED"
(Source lines and the stack trace are omitted.)
The message suggests using Bun's built-in bun:sqlite instead. A script that does the same thing with node:sqlite (DatabaseSync), which is built into Node.js 22, ran unchanged on Bun and returned the same two rows as Node.js (tama and mike). On Node.js 22, a warning that the feature is experimental is also printed. If you want code that runs on both Node.js and Bun, rewriting it to use node:sqlite is one option.
Running from package.json scripts requires –bun
I added a scripts entry "show": "node print-runtime.js" and ran it with bun run show. The printed process.version was v22.23.4, and typeof Bun was undefined. In other words, even with bun run, the code ran on Node.js.
$ bun --bun run show
$ node print-runtime.js
process.version: v26.3.0
process.versions.bun: 1.4.4
typeof Bun: object
execPath basename: bun
With --bun, the displayed command is still node print-runtime.js, but the script ran on Bun. Writing bun run --bun show gives the same result. The Bun runtime documentation says that for CLIs with a #!/usr/bin/env node shebang (such as vite), Bun normally runs them with node, and runs them with Bun when --bun is given. In my test, a scripts entry that calls node directly behaved the same way. Do not assume a script runs on Bun just because you use bun run. Print process.versions.bun or similar values to confirm.
Error Display and Stack Trace Format
Scripts that end with an uncaught exception stopped with exit code 1 on both Node.js and Bun. The same was true for unhandled Promise rejections. The difference was in the display. Bun printed the lines around the source with line numbers, and it did not include Node.js internal functions such as ModuleJob.run in the stack trace.
$ bun 09-errors.mjs
1 | // Script that ends with an uncaught exception
2 | function load() { throw new TypeError('config not found'); }
^
TypeError: config not found
at load (/path/to/app/09-errors.mjs:2:29)
at /path/to/app/09-errors.mjs:3:1
Bun v1.4.4 (Linux x64)
(The folder in my test environment is replaced with /path/to/app.) The column numbers also differed: Node.js showed 2:25, while Bun showed 2:29. If your code branches on the text of log messages, check that part again against Bun's output.





