温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.bun.com/docs/runtime/module-resolution
点击访问原文链接

Module Resolution - Bun

Module Resolution - BunDocumentation Index

Fetch the complete documentation index at: /docs/llms.txt

Use this file to discover all available pages before exploring further.

Skip to main contentBun home pageSearch...⌘KInstall BunSearch...NavigationFile & Module SystemModule ResolutionRuntimePackage ManagerBundlerTest RunnerGuidesReferenceBlogFeedback:first-child]:!hidden peer-[.is-custom]:[&>:first-child]:sm:!hidden peer-[.is-custom]:[&>:first-child]:md:!hidden peer-[.is-custom]:[&>:first-child]:lg:!hidden peer-[.is-custom]:[&>:first-child]:xl:!hidden">Get StartedWelcome to BunInstallationQuickstartTypeScriptTypeScript 6 and 7bun initbun createCore RuntimeBun RuntimeWatch ModeDebuggingREPLbunfig.tomlFile & Module SystemFile TypesModule ResolutionJSXAuto-installPluginsFile System RouterHTTP serverServerRoutingCookiesTLSError HandlingMetricsNetworkingFetchWebSocketsTCPUDPDNSData & StorageCookiesFile I/OStreamsBinary DataArchiveSQLSQLiteS3RedisConcurrencyWorkersProcess & SystemEnvironment VariablesShellSpawnWebViewCronInterop & ToolingNode-APIFFIC CompilerTranspilerUtilitiesCSRF ProtectionSecretsConsoleTOMLYAMLMarkdownJSON5JSONLHTMLRewriterImageHashingGlobSemverColorUtilsStandards & CompatibilityGlobalsBun APIsWeb APIsNode.js CompatibilityContributingRoadmapBenchmarkingContributingBuilding WindowsBindgenLicenseOn this pageSyntaxModule systemsUsing require()Using importUsing import and require() togetherTop level awaitImporting packagesNODE_PATHCustom conditionsPath re-mappingimport.metaFile & Module SystemModule ResolutionCopy pagespan]:line-clamp-1 overflow-hidden group flex items-center py-0.5 gap-1 text-sm text-gray-950/50 dark:text-white/50 group-hover:text-gray-950/70 dark:group-hover:text-white/70 rounded-none rounded-r-xl border px-3 border-gray-200 aspect-square dark:border-white/[0.07] bg-background-light dark:bg-background-dark hover:bg-gray-600/5 dark:hover:bg-gray-200/5" aria-label="More actions" type="button" id="radix-_R_n4ctdbsnlht5lebsnpfdb_" aria-haspopup="menu" aria-expanded="false" data-state="closed">*]:[overflow-wrap:anywhere]">

How Bun resolves modules and handles imports in JavaScript and TypeScript

Copy pagespan]:line-clamp-1 overflow-hidden group flex items-center py-0.5 gap-1 text-sm text-gray-950/50 dark:text-white/50 group-hover:text-gray-950/70 dark:group-hover:text-white/70 rounded-none rounded-r-xl border px-3 border-gray-200 aspect-square dark:border-white/[0.07] bg-background-light dark:bg-background-dark hover:bg-gray-600/5 dark:hover:bg-gray-200/5" aria-label="More actions" type="button" id="radix-_R_1cctdbsnlht5lebsnpfdb_" aria-haspopup="menu" aria-expanded="false" data-state="closed">Module resolution in JavaScript is a complex topic. The ecosystem is currently in the midst of a years-long transition from CommonJS modules to native ES modules. TypeScript enforces its own set of rules around import extensions that aren’t compatible with ESM. Different build tools support path re-mapping via disparate non-compatible mechanisms. Bun aims to provide a consistent and predictable module resolution system that works out of the box. Unfortunately it’s still quite complex. ​Syntax Consider the following files. index.tshello.ts
import { hello } from "./hello";

hello();
When we run index.ts, it prints “Hello world!”. terminal
bun index.ts
Hello world!
In this case, we are importing from ./hello, a relative path with no extension. Extensioned imports are optional but supported. To resolve this import, Bun will check for the following files in order: ./hello.tsx ./hello.jsx ./hello.ts ./hello.mjs ./hello.js ./hello.cjs ./hello.json ./hello/index.tsx ./hello/index.jsx ./hello/index.ts ./hello/index.mjs ./hello/index.js ./hello/index.cjs ./hello/index.json Import paths can optionally include extensions. If an extension is present, Bun will only check for a file with that exact extension. index.ts
import { hello } from "./hello";
import { hello } from "./hello.ts"; // this works
If you import from "*.js{x}", Bun will additionally check for a matching *.ts{x} file, to be compatible with TypeScript’s ES module support. index.ts
import { hello } from "./hello";
import { hello } from "./hello.ts"; // this works
import { hello } from "./hello.js"; // this also works
Bun supports both ES modules (import/export syntax) and CommonJS modules (require()/module.exports). The following CommonJS version would also work in Bun. index.jshello.js
const { hello } = require("./hello");

hello();
That said, using CommonJS is discouraged in new projects. ​Module systems Bun has native support for CommonJS and ES modules. ES Modules are the recommended module format for new projects, but CommonJS modules are still widely used in the Node.js ecosystem. In Bun’s JavaScript runtime, require can be used by both ES Modules and CommonJS modules. If the target module is an ES Module, require returns the module namespace object (equivalent to import * as). If the target module is a CommonJS module, require returns the module.exports object (as in Node.js). Module Typerequire()import * asES ModuleModule NamespaceModule NamespaceCommonJSmodule.exportsdefault is module.exports, keys of module.exports are named exports ​Using require() You can require() any file or package, even .ts or .mjs files. index.ts
const { foo } = require("./foo"); // extensions are optional
const { bar } = require("./bar.mjs");
const { baz } = require("./baz.tsx");

What is a CommonJS module?

In 2016, ECMAScript added support for ES Modules. ES Modules are the standard for JavaScript modules. However, millions of npm packages still use CommonJS modules.CommonJS modules are modules that use module.exports to export values. Typically, require is used to import CommonJS modules.my-commonjs.cjs
const stuff = require("./stuff");
module.exports = { stuff };
The biggest difference between CommonJS and ES Modules is that CommonJS modules are synchronous, while ES Modules are asynchronous. There are other differences too. ES Modules support top-level await and CommonJS modules don’t. ES Modules are always in strict mode, while CommonJS modules are not. Browsers do not have native support for CommonJS modules, but they do have native support for ES Modules via

Module Resolution - Bun,AI智能索引,全网链接索引,智能导航,网页索引

    How Bun resolves modules and handles imports in JavaScript and TypeScript