Skip to content
Tutorials Center
My learning

Topics

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.

TypeScript
// math.cts
function add(a: number, b: number): number {
  return a + b;
}
export = add;

Try it

TypeScript export = is rejected in an ES module

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.

TypeScript import = require and esModuleInterop-style imports

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

TypeScript
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 = x is error TS1203: Export assignment cannot be used when targeting ECMAScript modules. It is allowed in .cts files 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 under nodenext, but it is a TypeScript-only construct that Node's type stripping rejects.
  • With esModuleInterop or allowSyntheticDefaultImports, import x from "m" is allowed for an export = module and means the same value. Without them, you must use import * as x or import x = require().

Usage notes

  • Prefer export default and named exports in new code. export = forces importers to use special syntax or interop flags.
  • When describing a CommonJS library, write export = fn; plus a declare namespace fn { ... } of the same name to attach types for options and results.
  • With esModuleInterop, a namespace import (import * as x) of an export = 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 = with export default, then update importers to default imports.
  • .cts and .mts extensions pick CommonJS or ES module semantics per file under node16 and nodenext.