{% callout %} **Note** — Added in Bun v0.3.0 {% /callout %} 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](/docs/cli/install#global-cache) during execution (the same cache used by [`bun install`](/docs/cli/install)). ```ts 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. ## Version resolution To determine which version to install, Bun follows the following algorithm: 1. Check for a `bun.lockb` file in the project root. If it exists, use the version specified in the lockfile. 2. Otherwise, scan up the tree for a `package.json` that includes `"foo"` as a dependency. If found, use the specified semver version or version range. 3. Otherwise, use `latest`. ## Cache behavior Once a version or version range has been determined, Bun will: 1. Check the module cache for a compatible version. If one exists, use it. 2. When resolving `latest`, Bun will check if `package@latest` has been downloaded and cached in the last _24 hours_. If so, use it. 3. Otherwise, download and install the appropriate version from the `npm` registry. ## Installation Packages are installed and cached into `/@`, so multiple versions of the same package can be cached at once. Additionally, a symlink is created under `//` to make it faster to look up all versions of a package that exist in the cache. ## Version specifiers This entire resolution algorithm can be short-circuited by specifying a version or version range directly in your import statement. ```ts import { z } from "zod@3.0.0"; // specific version import { z } from "zod@next"; // npm tag import { z } from "zod@^3.20.0"; // semver range ``` ## Benefits This auto-installation approach is useful for a few reasons: - **Space efficiency** — Each version of a dependency only exists in one place on disk. This is a huge space and time savings compared to redundant per-project installations. - **Portability** — To share simple scripts and gists, your source file is _self-contained_. No need to `zip` together a directory containing your code and config files. With version specifiers in `import` statements, even a `package.json` isn't necessary. - **Convenience** — There's no need to run `npm install` or `bun install` before running a file or script. Just `bun run` it. - **Backwards compatibility** — Because Bun still respects the versions specified in `package.json` if one exists, you can switch to Bun-style resolution with a single command: `rm -rf node_modules`. ## Limitations - No Intellisense. TypeScript auto-completion in IDEs relies on the existence of type declaration files inside `node_modules`. We are investigating various solutions to this. - No [patch-package](https://github.com/ds300/patch-package) support ## FAQ {% details summary="How is this different from what pnpm does?" %} With pnpm, you have to run `pnpm install`, which creates a `node_modules` folder of symlinks for the runtime to resolve. By contrast, Bun resolves dependencies on the fly when you run a file; there's no need to run any `install` command ahead of time. Bun also doesn't create a `node_modules` folder. {% /details %} {% details summary="How is this different from Yarn Plug'N'Play does?" %} With Yarn, you must run `yarn install` before you run a script. By contrast, Bun resolves dependencies on the fly when you run a file; there's no need to run any `install` command ahead of time. 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. {% /details %} {% details summary="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. {% /details %} option value='jarred/actions'>jarred/actions Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
AgeCommit message (Expand)AuthorFilesLines
2022-02-24Update MakefileGravatar Jarred Sumner 1-1/+0
2022-02-24Update WebKitGravatar Jarred Sumner 1-0/+0
2022-02-24Update generated versionsGravatar Jarred Sumner 1-2/+2
2022-02-24Update MakefileGravatar Jarred Sumner 1-1/+3
2022-02-24miscGravatar Jarred Sumner 2-4/+4
2022-02-24[bun dev] Don't log errors twiceGravatar Jarred Sumner 1-1/+0
2022-02-24Add a simple HMR testGravatar Jarred Sumner 1-2/+40
2022-02-24Make alignment more consitentGravatar Jarred Sumner 3-4/+56
2022-02-24[bun dev] Fix bug with not transpiling files at the rootGravatar Jarred Sumner 2-21/+47
2022-02-24slightly clean up react exampleGravatar Jarred Sumner 5-55/+2
2022-02-24[bun-framework-next] Support Next.js 12.1Gravatar Jarred Sumner 6-25/+46
2022-02-24[bun.js] Implement `process.exit` (no callbacks yet)Gravatar Jarred Sumner 2-0/+29
2022-02-24[bun install] Print correct bin nameGravatar Jarred Sumner 1-1/+1
2022-02-24[bun install] Add integration test for bin linksGravatar Jarred Sumner 1-1/+7
2022-02-24Add WASM modules but disable it for nowGravatar Jarred Sumner 28-134/+530
2022-02-24bump build idGravatar Jarred Sumner 1-1/+1
2022-02-24fix test failure in path.resolveGravatar Jarred Sumner 1-2/+6
2022-02-24Ensure we run the process testGravatar Jarred Sumner 2-48/+54
2022-02-24Update javascript.zigGravatar Jarred Sumner 1-1/+110
2022-02-24[bun.js] Add `ShadowRealm`Gravatar Jarred Sumner 7-18/+70
2022-02-24[bun-framework-next] Remove TextEncoder & TextDecoder polyfillsGravatar Jarred Sumner 3-340/+19
2022-02-24Use a JSFinalobject for PathGravatar Jarred Sumner 2-81/+42
2022-02-24Expose TextEncoder & TextDecoder globallyGravatar Jarred Sumner 3-20/+750
2022-02-24[Web Platform] Implement TextEncoder & TextDecoderGravatar Jarred Sumner 12-358/+1044
2022-02-24move GCDeferralContextGravatar Jarred Sumner 2-18/+24
2022-02-24[JS Parser] ensure assertions are never run at runtimeGravatar Jarred Sumner 1-13/+18
2022-02-24misc cleanupGravatar Jarred Sumner 2-7/+18
2022-02-22Make format consistent with WebKitGravatar Jarred Sumner 20-3596/+4110
2022-02-22import assertion testGravatar Jarred Sumner 1-0/+33
2022-02-22snaspshotsGravatar Jarred Sumner 42-94/+113