liveBook · Manning

Module setting

As explained in Essential TypeScript 5, Third Edition

A module setting is a TypeScript compiler configuration option that selects the module format emitted for a project.

A module setting is a TypeScript compiler option in tsconfig.json that selects the module format emitted for a project. It determines how imports and exports appear in generated JavaScript, helping the output match its runtime or delivery toolchain. section 5.8.1

For Node.js projects, Node16 uses the type property in package.json, or the .mts and .cts extensions, to determine the module format. This reduces mismatches between TypeScript output and Node.js behavior. section 5.8.1 section 15.1

Node16 configuration

In tsconfig.json, configure a Node.js-oriented project with "module": "Node16". With package.json containing "type": "module", an import such as import { sum } from "./calc.js" remains an ECMAScript module import. Changing package.json to "type": "commonjs" causes the compiler to emit CommonJS code using require instead.

Web applications often receive their module-format decision from a framework toolchain, bundler, or module loader. The module setting may be omitted when defaults are suitable, but explicitly selecting Node16 makes the runtime relationship clearer. section 5.8.1

How Essential TypeScript 5, Third Edition covers it

How deep it goes
Explains format selection, package.json interaction, and generated import changes, while deferring detailed module resolution.
Examples
Demonstrates Node16 configuration, package.json type changes, import syntax, generated require calls, and a watched compiler project.
What it assumes
Assumes familiarity with TypeScript projects, tsconfig.json, package.json, imports, and exports while showing configuration from project setup.
What you can do after
Operate/configure TypeScript projects with a module format that matches the selected Node.js runtime or delivery toolchain.
How it connects
Connects module setting with tsconfig.json, package.json, Node.js, CommonJS, ECMAScript modules, bundlers, and module resolution.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Essential TypeScript 5, Third Edition. The section links point to the parts of the book it draws on.