"module"

The module format of the generated code and how imports are understood.

On this page

Example

#
JSON
{
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext"
  }
}

Definition and usage

#

The module option decides which module system the compiler writes imports and exports in: ES modules (ESNext, ES2022), CommonJS (CommonJS, meaning require), or the Node.js-compatible modes (Node16, NodeNext).

In browser applications with a bundler, ESNext together with moduleResolution: "Bundler" is the most common choice. In libraries and projects run directly in Node.js, NodeNext is recommended, because it respects the type field in package.json and file extensions.

A badly chosen setting shows up as errors like “Cannot use import statement outside a module” or “require is not defined” in a running program.

Syntax

#
Syntax
"module": "ESNext"

Good practices

#
  • For Node.js, set both module and moduleResolution to NodeNext.
  • In Vite or Next.js apps, keep the settings from the project template. They are chosen to suit the bundler.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.