skills/bun-runtime-auto-install/SKILL.md
Bun's automatic package installation feature for standalone script execution
npx skillsauth add jarle/bun-skills Bun Auto-installInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Bun's automatic package installation feature for standalone script execution
If no node_modules directory is found in the working directory or higher, Bun will abandon Node.js-style module resolution in favor of the Bun module resolution algorithm.
Under Bun-style module resolution, all imported packages are auto-installed on the fly into a global module cache during execution (the same cache used by bun install).
import { foo } from "foo"; // install `latest` version
foo();
The first time you run this script, Bun will auto-install "foo" and cache it. The next time you run the script, it will use the cached version.
To determine which version to install, Bun follows the following algorithm:
bun.lock file in the project root. If it exists, use the version specified in the lockfile.package.json that includes "foo" as a dependency. If found, use the specified semver version or version range.latest.Once a version or version range has been determined, Bun will:
latest, Bun will check if package@latest has been downloaded and cached in the last 24 hours. If so, use it.npm registry.Packages are installed and cached into <cache>/<pkg>@<version>, so multiple versions of the same package can be cached at once. Additionally, a symlink is created under <cache>/<pkg>/<version> to make it faster to look up all versions of a package that exist in the cache.
This entire resolution algorithm can be short-circuited by specifying a version or version range directly in your import statement.
import { z } from "[email protected]"; // specific version
import { z } from "zod@next"; // npm tag
import { z } from "zod@^3.20.0"; // semver range
This auto-installation approach is useful for a few reasons:
zip together a directory containing your code and config files. With version specifiers in import statements, even a package.json isn't necessary.npm install or bun install before running a file or script. Just bun run it.package.json if one exists, you can switch to Bun-style resolution with a single command: rm -rf node_modules.node_modules. We are investigating various solutions to this.Yarn Plug'N'Play also uses zip files to store dependencies. This makes dependency loading [slower at runtime](https://twitter.com/jarredsumner/status/1458207919636287490), as random access reads on zip files tend to be slower than the equivalent disk lookup.
</Accordion>
<Accordion title="How is this different from what Deno does?">
Deno requires an `npm:` specifier before each npm `import`, lacks support for import maps via `compilerOptions.paths` in `tsconfig.json`, and has incomplete support for `package.json` settings. Unlike Deno, Bun does not currently support URL imports.
</Accordion>
</AccordionGroup>development
Using TypeScript with Bun, including type definitions and compiler options
development
Learn how to write tests using Bun's Jest-compatible API with support for async tests, timeouts, and various test modifiers
testing
Learn how to use snapshot testing in Bun to save and compare output between test runs
testing
Learn about Bun test's runtime integration, environment variables, timeouts, and error handling