-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Description
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:
- When
nodeis not in path,bun <any-executable>andbun run <any-executable>automatically adds a temporarynodeto path symlinked to Bun, which makes#!/usr/bin/env nodestill work when Node is not installed - The
--bunflag forces the behavior of ^ even whennodeis 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)