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 just works. Unfortunately it's still quite complex. ## Syntax Consider the following files. {% codetabs %} ```ts#index.ts import { hello } from "./hello"; hello(); ``` ```ts#hello.ts export function hello() { console.log("Hello world!"); } ``` {% /codetabs %} When we run `index.ts`, it prints "Hello world!". ```bash $ 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.ts` - `./hello.tsx` - `./hello.js` - `./hello.mjs` - `./hello.cjs` - `./hello/index.ts` - `./hello/index.js` - `./hello/index.json` - `./hello/index.mjs` Import paths are case-insensitive, meaning these are all valid imports: ```ts#index.ts import { hello } from "./hello"; import { hello } from "./HELLO"; import { hello } from "./hElLo"; ``` Import paths can optionally include extensions. If an extension is present, Bun will only check for a file with that exact extension. ```ts#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](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#new-file-extensions). ```ts#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. {% codetabs %} ```ts#index.js const { hello } = require("./hello"); hello(); ``` ```ts#hello.js function hello() { console.log("Hello world!"); } exports.hello = hello; ``` {% /codetabs %} 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 Type | `require()` | `import * as` | | ----------- | ---------------- | ----------------------------------------------------------------------- | | ES Module | Module Namespace | Module Namespace | | CommonJS | module.exports | `default` is `module.exports`, keys of module.exports are named exports | ### Using `require()` You can `require()` any file or package, even `.ts` or `.mjs` files. ```ts const { foo } = require("./foo"); // extensions are optional const { bar } = require("./bar.mjs"); const { baz } = require("./baz.tsx"); ``` {% details summary="What is a CommonJS module?" %} In 2016, ECMAScript added support for [ES Modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/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. ```ts // 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/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 `