日本語

bun.lock vs package-lock.json in Bun and npm

​​Bun​Node.js,npm,package management

bun.lock and package-lock.json both record the versions of installed packages and their integrity hashes (hashes of the package contents). The recorded information is nearly the same, but the format differs, and so does whether each tool reads the other's file. Bun can read package-lock.json, but npm does not read bun.lock and reinstalls from package.json instead.

This article covers how to handle lockfiles when moving from npm to Bun, or when you want to pin dependency versions in CI. I created two lockfiles from the same package.json in my test environment and compared their contents and behavior.

Note

The test environment was Ubuntu 24.04 (x86_64), Node.js 22.23.4 (npm 10.9.9), and Bun 1.4.4. Packages were fetched from registry.npmjs.org. I did not test projects that use workspaces, git: or file: dependencies, the old binary bun.lockb format, Windows, or macOS. I checked the official Bun and npm documentation against the source files at the versions I tested (Bun tag bun-v1.4.4, npm tag v10.9.9) on GitHub.

Two files created from the same package.json

I placed a package.json with only two dependencies in one folder for npm and another for Bun.

json
{
  "name": "lock-demo",
  "version": "1.0.0",
  "private": true,
  "dependencies": {
    "debug": "^4.3.7",
    "chalk": "^5.3.0"
  }
}

I ran npm install in one folder and bun install in the other. Both installed three packages: chalk 5.6.2, debug 4.4.3, and ms 2.1.3, which debug depends on. The sizes of the resulting lockfiles were as follows.

text
$ wc -c -l n/package-lock.json b/bun.lock
  51 1457 n/package-lock.json
  20  751 b/bun.lock

package-lock.json had 51 lines and 1,457 bytes, and bun.lock had 20 lines and 751 bytes. Here is the full bun.lock:

text
{
  "lockfileVersion": 2,
  "configVersion": 1,
  "workspaces": {
    "": {
      "name": "lock-demo",
      "dependencies": {
        "chalk": "^5.3.0",
        "debug": "^4.3.7",
      },
    },
  },
  "packages": {
    "chalk": ["chalk@5.6.2", "", {}, "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA=="],

    "debug": ["debug@4.4.3", "", { "dependencies": { "ms": "^2.1.3" }, "peerDependencies": { "supports-color": "*" }, "optionalPeers": ["supports-color"] }, "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA=="],

    "ms": ["ms@2.1.3", "", {}, "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="],
  }
}

How a single package is written

For the same ms 2.1.3, package-lock.json looked like this:

json
"node_modules/ms": {
  "version": "2.1.3",
  "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
  "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
  "license": "MIT"
}

In bun.lock, each package is a single-line array:

text
"ms": ["ms@2.1.3", "", {}, "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA=="],

Comparing the two, there were three differences:

Item package-lock.json bun.lock
Key node_modules/ms (the install path) ms (the package name)
Download URL resolved holds the tarball URL The second element is an empty string, so no URL is written
License and supported Node.js versions license is written (and engines for chalk and debug) Not written

The integrity values were exactly the same for all three packages in both files. The package-lock.json official documentation also states that each key in packages is a path relative to the project folder, which explains the key difference.

The other difference is that bun.lock puts a trailing comma after the last element. Because of this, Node.js's JSON.parse cannot read it:

text
$ node -e 'JSON.parse(require("fs").readFileSync("b/bun.lock","utf8")); console.log("ok")'
(中略)
SyntaxError: Expected double-quoted property name in JSON at position 187 (line 10 column 7)
(以下、スタックトレースが続くので省略)

package-lock.json could be read with the same command. In the Bun folder, running Bun with require("./bun.lock") loaded the file, and I could extract lockfileVersion and the keys of packages:

text
$ bun -e 'const l = require("./bun.lock"); console.log(l.lockfileVersion, Object.keys(l.packages))'
2 [ "chalk", "debug", "ms" ]

The diff when adding one dependency

I committed each folder to git once, then added picocolors 1.1.1 with npm install picocolors and bun add picocolors. The git diff --stat output for the npm folder was:

text
$ git diff --stat
 package-lock.json | 9 ++++++++-
 package.json      | 3 ++-
 2 files changed, 10 insertions(+), 2 deletions(-)

For the Bun folder, it was:

text
$ git diff --stat
 bun.lock     | 3 +++
 package.json | 3 ++-
 2 files changed, 5 insertions(+), 1 deletion(-)

For the lockfiles, package-lock.json had 8 lines added and 1 removed, while bun.lock had only 3 lines added. The bun.lock diff is shown below (hashes are abbreviated):

diff
       "dependencies": {
         "chalk": "^5.3.0",
         "debug": "^4.3.7",
+        "picocolors": "^1.1.1",
       },
(中略)
     "ms": ["ms@2.1.3", "", {}, "sha512-6Flz…"],
+
+    "picocolors": ["picocolors@1.1.1", "", {}, "sha512-xceH…"],

The one removed line in package-lock.json was "debug": "^4.3.7". Because the picocolors line was added after it, that line was rewritten with a trailing comma. bun.lock already has a trailing comma on its last line, so the existing line did not change, and only the added lines appear in the diff.

When package.json and the lockfile do not match

In CI, you typically install exactly what the lockfile specifies and fail if it does not match package.json. With npm, use npm ci; with Bun, use bun ci. According to the official bun install documentation, bun ci behaves the same as bun install --frozen-lockfile.

I manually added "is-number": "^7.0.0" to the dependencies in package.json without updating the lockfile, then ran both commands:

text
$ npm ci --no-audit --no-fund
npm error code EUSAGE
npm error
npm error `npm ci` can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with `npm install` before continuing.
npm error
npm error Missing: is-number@7.0.0 from lock file
(使い方の説明が続くので省略)
text
$ bun ci
bun install v1.4.4 (38afee6c8)
error: lockfile had changes, but lockfile is frozen
note: dependencies in package.json changed since bun.lock was saved: is-number was added
note: try re-running without --frozen-lockfile and commit the updated lockfile

Both stopped with exit code 1 and did not modify the lockfile. Only package.json showed as changed in git status. bun install --frozen-lockfile printed the same message as bun ci.

Next, instead of adding a dependency, I only changed the debug range from ^4.3.7 to >=4.3.7. The debug 4.4.3 in the lockfile satisfies both ranges. In this case, both npm ci and bun ci completed the install without stopping:

text
$ bun ci
bun install v1.4.4 (38afee6c8)

Checked 4 installs across 5 packages (no changes) [1.00ms]

The same official Bun documentation also says that changes which do not alter the installed version, such as changing ^1.0.0 to >=1.0.0, do not cause a failure.

npm does not read bun.lock

I created a folder where bun.lock pins chalk to 5.3.0, then installed with npm and with Bun. The range in package.json is ^5.3.0, and the registry has 5.6.2, which falls within that range.

text
$ grep -A3 '"packages"' bun.lock
  "packages": {
    "chalk": ["chalk@5.3.0", "", {}, "sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w=="],
  }
}

I copied this folder into two, deleted node_modules, and ran npm install in one and bun ci in the other. The installed chalk versions were:

text
$ npm install --no-audit --no-fund && node -p 'require("./node_modules/chalk/package.json").version'

added 1 package in 256ms
5.6.2
text
$ bun ci && node -p 'require("./node_modules/chalk/package.json").version'
bun install v1.4.4 (38afee6c8)

+ chalk@5.3.0

1 package installed [1.00ms]
5.3.0

npm ignored bun.lock, picked a version from the package.json range, and installed 5.6.2. A new package-lock.json was created in the folder, and bun.lock remained as well. When I ran npm ci in a folder that had only bun.lock, it stopped with an error saying package-lock.json was missing:

text
$ npm ci --no-audit --no-fund
npm error code EUSAGE
npm error
npm error The `npm ci` command can only install with an existing package-lock.json or
npm error npm-shrinkwrap.json with lockfileVersion >= 1. Run an install with npm@5 or
npm error later to generate a package-lock.json file, then try again.
(使い方の説明が続くので省略)
Warning

In a project that commits only bun.lock, if someone runs npm install, a version different from the one pinned in the lockfile may be installed. Make sure everyone on the project uses the same package manager, either npm or Bun.

Bun reads package-lock.json

Conversely, when I ran bun ci in a folder that had only package.json and package-lock.json, Bun read package-lock.json and installed from it:

text
$ bun ci
bun install v1.4.4 (38afee6c8)
[0.32ms] migrated lockfile from package-lock.json

+ chalk@5.6.2
+ debug@4.4.3
+ picocolors@1.1.1

4 packages installed [2.00ms]

According to the official Bun lockfile documentation, bun ci and bun install --frozen-lockfile convert the package-lock.json they read into bun.lock in memory and use it, but do not write that file to disk. To create bun.lock, run bun install without flags or run bun pm migrate. In either case, the original package-lock.json is kept. The same documentation says that since Bun 1.2, the text-format bun.lock is the default. For the older binary bun.lockb, it describes converting to bun.lock with bun install --save-text-lockfile --frozen-lockfile --lockfile-only and then deleting bun.lockb.

In my test environment, the folder after bun ci contained only package.json, package-lock.json, and node_modules. No bun.lock had been created.

​Posted by 86