TypeScript Reference ยท Keywords and Declarations
The export = and import = syntax
Reference for TypeScript export = and import x = require(): the CommonJS module syntax, esModuleInterop, and why ES module projects reject export =.
export = value is the TypeScript form of CommonJS module.exports = value. It makes a single value the whole module, rather than a set of named exports. The matching import is import x = require("module"), which compiles to a plain require call when the output is CommonJS.
Both belong to the CommonJS world. In a project that emits ES modules, export = is an error, while import x = require() is still accepted in a limited way. New code should use export default and import x from; you meet export = mostly in .d.ts files describing old packages, such as a function that also carries properties.
// math.cts function add(a: number, b: number): number { return a + b; } export = add;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
export = expression; // CommonJS: module.exports = expression import name = require("module"); // CommonJS: const name = require("module") import Alias = Namespace.Member; // alias, not a module import export import Alias = Other.Thing; // re-exported alias export as namespace GlobalName; // in .d.ts: UMD global
| Form | Description |
|---|---|
export = x |
Replaces the module's exports with x. The file cannot have other top-level exports. |
import x = require("m") |
Imports a module that uses export =. Gives you the whole value. |
export as namespace G |
Declaration-file form that also exposes the module as a global for script-tag use. |
Edge cases and errors:
- In a file that targets ECMAScript modules,
export = xis error TS1203: Export assignment cannot be used when targeting ECMAScript modules. It is allowed in.ctsfiles and in CommonJS projects. - A file with
export =cannot also have other exports. - An
export =target that is a class or function merged with a namespace can expose both a callable value and extra types. import x = require()in an ES-module file works undernodenext, but it is a TypeScript-only construct that Node's type stripping rejects.- With
esModuleInteroporallowSyntheticDefaultImports,import x from "m"is allowed for anexport =module and means the same value. Without them, you must useimport * as xorimport x = require().
Usage notes
- Prefer
export defaultand named exports in new code.export =forces importers to use special syntax or interop flags. - When describing a CommonJS library, write
export = fn;plus adeclare namespace fn { ... }of the same name to attach types for options and results. - With
esModuleInterop, a namespace import (import * as x) of anexport =module whose value is a function or class cannot be called, because a namespace object is not callable; use the default import instead. - Moving to ESM: replace
export =withexport default, then update importers to default imports. .ctsand.mtsextensions pick CommonJS or ES module semantics per file undernode16andnodenext.