Skip to content

Use the "engines" field to automatically replace node with bun in the shebang #9346

@Jarred-Sumner

Description

@Jarred-Sumner

Currently, it is difficult to publish CLIs to npm which can transparently run in either node or bun.

Thus far, Bun has attempted to address that in two ways:

  1. When node is not in path,bun <any-executable> and bun run <any-executable> automatically adds a temporary node to path symlinked to Bun, which makes #!/usr/bin/env node still work when Node is not installed
  2. The --bun flag forces the behavior of ^ even when node is in path - bun --bun <any-executable>

This doesn't fix the case where <any-executable> is installed globally, you don't have node installed and you want to run it without prefixing bun.

While some packages are happy to be bun-only, for many packages it doesn't make sense today to limit their potential users to only bun.

tsc and turbo both seem to work in bun, there's no reason it doesn't run them using bun when bun is installed

This proposal suggests the following:

When using bun install and "bun" exists in the "engines" field in package.json in an installed dependency, we automatically inject and replace the #!/usr/bin/env node shebang with #!/usr/bin/env bun in all bins with a javascript-like file extension.

{
  "name": "my-cli-tool",
  "version": "1.0.0",
  "engines": {
    "node": "*",
    "bun": "*"
  },
  "bin": {
    "my-cli": "./bin/my-cli.js"
  }
}

This would let package authors publish packages for both node and bun that lets the user choose which runtime to use (implicitly by choosing bun with bun install, or node with other package managers). This has the downside that yarn v1 will report a warning about an unknown engine (only yarn v1 as far as I can tell), but that's probably okay (the "vscode" engine used by vscode extensions has the same issue)

Another approach involves the "exports" field, but the npm registry api does not expose that information in the abbreviated version object (nor should it, imo)

Fixes #9334
Blocked on #5846

Metadata

Metadata

Assignees

Labels

bun installSomething that relates to the npm-compatible clientenhancementNew feature or requesttrackingAn umbrella issue for tracking big features

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions