TypeScripttsconfig options
"module"
The module format of the generated code and how imports are understood.
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
moduleandmoduleResolutiontoNodeNext. - In Vite or Next.js apps, keep the settings from the project template. They are chosen to suit the bundler.
Related entries
#- "moduleResolution"The algorithm the compiler uses to find the files named in imports.
- "target"The JavaScript version the compiler turns your code into.
- "esModuleInterop"Makes it easier to import CommonJS packages with ES module syntax.
- "verbatimModuleSyntax"Imports without the word type stay in JavaScript unchanged, so type imports must be marked explicitly.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.