How Do target and module Work in TypeScript?

Learn How Do target and module Work in TypeScript? with practical examples, explanations, best practices, and real-world coding techniques.

Introduction to Target and Module Options

TypeScript allows developers to write JavaScript with static types, but a project must also determine what kind of JavaScript should be generated and how imported modules should be handled.

Two important compiler options control these decisions: target and module. The target option specifies the ECMAScript language version TypeScript aims to emit when transforming supported syntax. The module option controls module output behavior and, in modern TypeScript, influences how module-related code is interpreted and checked.

For example, a frontend application built with a bundler may preserve modern ECMAScript imports and exports, while a Node.js application may need output that follows Node.js module rules.

Choosing the correct settings helps prevent compatibility problems between your source code, compiler, build tools, and production runtime. Understanding these options also makes it easier to diagnose import errors and unexpected JavaScript output.

1. Understanding the target Option

The target option specifies the ECMAScript version that TypeScript should aim for when emitting JavaScript. It influences whether supported language features are preserved or transformed into syntax compatible with an older target.

{
  "compilerOptions": {
    "target": "ES2022"
  }
}

ES2022 is a modern ECMAScript target. TypeScript can preserve language features supported by this target instead of transforming every supported feature into older syntax.

Common target values include ES5, ES2015, ES2020, ES2022, and ESNext. The suitable value depends on the JavaScript environments your application needs to support.

A modern browser application may use a recent ECMAScript target, while an application supporting older browsers may require additional transformations through its build toolchain.

The target setting does not automatically install polyfills or make unavailable runtime APIs work. If an older runtime does not support a method or browser API, you may need a compatible alternative or an appropriate polyfill.

2. Common target Values

Different ECMAScript targets represent different compatibility levels. Understanding the general purpose of these targets helps you choose an appropriate configuration.

Target Typical purpose
ES5 Older JavaScript syntax compatibility when supported transformations are available.
ES2015 Modern JavaScript fundamentals, including classes, modules, and arrow functions.
ES2020 A more recent syntax baseline with features such as optional chaining, nullish coalescing, and dynamic import support.
ES2022 A modern baseline including features such as class static initialization blocks and top-level await in modules.
ESNext Targets the latest ECMAScript features supported by the installed TypeScript version; the target can evolve as TypeScript is updated.

These descriptions provide a general guide rather than a complete compatibility specification. The exact syntax transformed by the compiler depends on the selected target and TypeScript version.

For production applications, select a target based on your supported environments rather than choosing the newest value automatically.

3. What Is the module Option?

JavaScript modules allow applications to divide functionality across files using import and export statements. The module option tells TypeScript how to handle this module syntax during compilation and helps determine the module-related behavior against which the code is checked.

Common choices include CommonJS, ESNext, NodeNext, and Preserve. These choices are not interchangeable because different runtimes and build tools expect different module behavior.

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext"
  }
}

With ESNext, TypeScript preserves modern ECMAScript module syntax in its emitted JavaScript when emission is enabled. A bundler can then process the imports and exports according to its own configuration.

This is a common approach in frontend applications because tools such as Vite and other bundlers handle module processing, dependency optimization, and final production bundles.

Module behavior also affects type checking and dependency resolution. With modern module settings, TypeScript considers factors such as how a file will run, package export conditions, and the module system used by the dependency.

4. CommonJS Modules

CommonJS is a module system historically associated with Node.js. It uses require to load modules and module.exports or exports to expose values.

TypeScript can transform standard import and export syntax into CommonJS output when the module option is configured accordingly.

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "CommonJS",
    "strict": true,
    "outDir": "./dist"
  },
  "include": ["src/**/*.ts"]
}

Suppose a TypeScript file contains the following import and export statements:

import { add } from "./math";

export function calculateTotal(): number {
  return add(10, 20);
}

With CommonJS output, TypeScript transforms module syntax into the corresponding CommonJS form when supported by the compiler settings.

CommonJS may be appropriate for legacy Node.js projects or environments explicitly configured to use it. However, modern Node.js supports both CommonJS and ECMAScript modules, so a project should not select CommonJS merely because it runs on Node.js.

5. ESNext Modules

ESNext is a module setting commonly used when a bundler should process ECMAScript imports and exports. It preserves modern module syntax rather than converting every module import into CommonJS require calls.

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "strict": true,
    "noEmit": true
  },
  "include": ["src/**/*.ts"]
}

This configuration is a common starting point for a bundler-based application. TypeScript checks the code but does not emit JavaScript because noEmit is enabled.

The bundler is then responsible for transforming, resolving, and packaging the application's modules.

ESNext represents the latest ECMAScript module capabilities supported by the installed TypeScript version. It is not a guarantee that every target runtime natively supports every feature. Your build pipeline and runtime requirements must still be considered.

Bundler-based module output and Node.js module output can behave differently when resolving relative paths and package exports. Use a module resolution strategy that matches the actual tool running the code.

6. NodeNext for Modern Node.js Applications

Node.js supports both ECMAScript modules and CommonJS modules. The NodeNext module setting allows TypeScript to model Node.js module behavior rather than forcing the entire project into one module format.

{
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "rootDir": "./src",
    "outDir": "./dist",
    "strict": true
  },
  "include": ["src/**/*.ts"]
}

NodeNext determines module behavior using Node.js conventions, including file extensions and the nearest applicable package.json metadata. A project's package type and its source file extensions can affect whether the emitted JavaScript uses ESM or CommonJS.

For example, a package configured with type: module in package.json treats ordinary .js files as ECMAScript modules. NodeNext allows TypeScript to account for that module format when checking and compiling supported source files.

{
  "type": "module",
  "scripts": {
    "build": "tsc",
    "start": "node dist/index.js"
  }
}

In Node.js ESM, relative import specifiers generally need the extension that will exist at runtime. A TypeScript source file can therefore use a .js specifier when importing a corresponding TypeScript source file that will be emitted as JavaScript.

// src/index.ts
import { add } from "./math.js";

console.log(add(10, 20));

TypeScript can resolve this specifier to the corresponding TypeScript source file during compilation, while the emitted JavaScript retains a specifier that Node.js can resolve at runtime.

NodeNext behavior tracks modern Node.js module rules. If your project needs a fixed, historical version of Node.js module behavior, investigate the supported version-specific module settings in your installed TypeScript release.

7. Understanding moduleResolution

The moduleResolution option determines how TypeScript finds modules referenced by imports. It works closely with the module option but serves a different purpose.

Common modern choices include Bundler and NodeNext. Each models the rules used by a different module-loading environment.

{
  "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "Bundler"
  }
}

Bundler resolution is designed for modern bundlers. It supports package.json imports and exports conventions while allowing extensionless relative imports in situations where a bundler supports them.

NodeNext resolution models Node.js's module rules, including stricter requirements for relative ESM import specifiers. The algorithm used can depend on whether the relevant code will be executed as an import or require operation.

These modes should not be mixed casually. NodeNext module resolution must be paired with a compatible NodeNext module setting. Bundler resolution is intended for bundler-based projects and typically pairs with ESNext or another compatible modern module setting.

Choosing the wrong resolution mode can allow imports that type-check successfully but fail when the generated application runs. The best configuration is the one that reflects the actual runtime or build tool.

8. Target and Module Are Different Settings

The target and module options are related but control different concerns. Changing one does not replace the need to choose the other.

Setting Main responsibility
target Determines the ECMAScript language level used for emitted JavaScript transformations.
module Controls module output behavior and influences module-related type checking and resolution.
moduleResolution Controls how module imports are located and interpreted.
lib Selects library declarations describing available built-in and platform APIs.
noEmit Prevents TypeScript from writing compiler output.

For example, target: ES2022 with module: CommonJS aims for modern JavaScript syntax while generating CommonJS-style module output. Target: ES2022 with module: ESNext preserves ECMAScript module syntax instead.

These examples illustrate the distinction, but the actual behavior of advanced module settings also depends on file extensions, package metadata, TypeScript version, and related compiler options.

9. The Preserve Module Option

Preserve is a module output option intended for build pipelines that need to retain the original module syntax written in each source file rather than converting all imports and exports into one module format.

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "Preserve",
    "noEmit": true
  }
}

In Preserve mode, ECMAScript imports and exports remain in the output when emission is performed, while TypeScript can handle supported CommonJS-style import and export assignments according to the mode's rules.

This setting is particularly relevant for toolchains that process individual imports and exports or support mixed module syntax. It is not the same as CommonJS and should not be selected just because an application uses Node.js.

Preserve mode implies bundler-style module resolution behavior. Before selecting it, confirm that the installed TypeScript version and the rest of the build pipeline support your intended workflow.

10. Choosing a Configuration for a Frontend Application

Frontend applications commonly use a framework or bundler that handles TypeScript transpilation and JavaScript packaging. In such projects, TypeScript may primarily provide type checking rather than generating the final JavaScript bundle.

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "strict": true,
    "noEmit": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*.ts"]
}

This is an illustrative configuration for a modern bundler-based application. The correct values for a specific framework may differ because framework tooling can impose its own configuration requirements.

noEmit prevents TypeScript from generating JavaScript output, while the bundler or framework build system is responsible for the actual application build.

The target should reflect your supported JavaScript environments. If the framework already generates separate TypeScript configurations for application and build tooling, preserve those boundaries rather than replacing every configuration with one generic file.

Always test a framework-specific production build after adjusting compiler settings. A standalone TypeScript check does not guarantee that the framework compiler and bundler will accept every configuration change.

11. Choosing a Configuration for Node.js

Node.js projects should select module settings according to the module system expected by the runtime. Modern Node.js supports both ESM and CommonJS, so the file format and package metadata matter.

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "rootDir": "./src",
    "outDir": "./dist",
    "strict": true,
    "sourceMap": true
  },
  "include": ["src/**/*.ts"]
}

This configuration demonstrates a project that uses NodeNext resolution and emits JavaScript to dist. The output module format depends on how TypeScript classifies each source file.

If the project uses ESM, package.json metadata and relative import specifiers must be consistent with Node.js ESM rules. If it uses CommonJS, configure the package and source files accordingly.

NodeNext is a model of modern Node.js module behavior rather than a synonym for ESM output. It can produce ESM or CommonJS depending on the module format determined for each file.

Also remember that recent TypeScript versions provide fixed-version Node module settings in addition to NodeNext. Consult the supported options for your installed compiler if you need to model a particular Node.js version instead of following the latest NodeNext behavior.

12. Common Mistakes with Target and Module

Assuming target controls module format

target determines the JavaScript syntax level, while module controls module behavior. You must select appropriate values for both concerns.

Choosing ESNext for every Node.js project

ESNext preserves modern module syntax, but it does not by itself model all Node.js module rules. Use a suitable Node module mode when Node.js executes the emitted files directly.

Confusing NodeNext with ESM-only output

NodeNext models Node.js's dual module system and can emit either ESM or CommonJS depending on file format detection and package metadata.

Ignoring relative import extensions

Node.js ESM generally requires runtime-compatible extensions on relative imports. An import that works under bundler resolution may not work when executed directly by Node.js.

Using a target unsupported by production environments

A modern target can preserve syntax that an older runtime cannot parse. Confirm the supported browser or Node.js versions and select a target compatible with the deployment environment.

Assuming successful type checking guarantees runtime success

Module resolution and type checking do not execute the application. Verify that dependencies, module formats, file extensions, and package exports work in the real build and runtime.

Best Practices for Target and Module Options

  • Choose target according to the actual browser or runtime compatibility requirements.
  • Keep target separate from module format decisions; each setting addresses a different concern.
  • Use ESNext with compatible bundler resolution when a frontend bundler owns module processing.
  • Use NodeNext when TypeScript needs to model modern Node.js module behavior.
  • Keep moduleResolution compatible with module and the runtime that will execute the code.
  • Understand how package.json type and source file extensions affect Node.js ESM and CommonJS behavior.
  • Use runtime-compatible relative import extensions when required by Node.js ESM.
  • Do not assume a target supplies APIs or polyfills that are unavailable in an older runtime.
  • Use noEmit when another tool handles JavaScript output, and confirm that tool supports the selected module settings.
  • Preserve framework-provided compiler settings when they are required by the application's build system.
  • Inspect compiler diagnostics and test the complete production build after changing module configuration.
  • Review the current TypeScript documentation when choosing among version-specific module settings.

Conclusion

The TypeScript target and module options are fundamental to producing JavaScript that matches an application's execution environment. Target controls the syntax level used for emitted code, while module and moduleResolution determine how imports, exports, and module behavior are handled.

Frontend applications typically rely on bundlers and often preserve modern module syntax. Node.js applications may need NodeNext behavior to account for ESM and CommonJS rules, file extensions, and package metadata.

Practise compiling the same small module with different configurations and inspect the resulting output. Once you understand the difference between syntax targets and module systems, you can choose configurations more confidently and troubleshoot import problems more efficiently.

Further Reading

Code Examples

Configuring a Modern JavaScript Target

json
{
  "compilerOptions": {
    "target": "ES2022",
    "strict": true
  }
}
    

The target option determines the JavaScript syntax level TypeScript aims to emit. Choose a target supported by the environments in which the application will run.

CommonJS Module Configuration

json
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "CommonJS",
    "strict": true,
    "outDir": "./dist"
  },
  "include": ["src/**/*.ts"]
}
    

CommonJS converts supported TypeScript import and export syntax into CommonJS-style JavaScript. It can be suitable for projects configured to use that module system.

Frontend Bundler Configuration

json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "strict": true,
    "noEmit": true
  },
  "include": ["src/**/*.ts"]
}
    

This setup lets TypeScript check the application while a bundler handles transpilation and module processing. Framework-specific requirements may require additional settings.

Node.js Module Configuration

json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "rootDir": "./src",
    "outDir": "./dist",
    "strict": true
  },
  "include": ["src/**/*.ts"]
}
    

NodeNext models modern Node.js module rules, including the interaction between import syntax, package metadata, and source file extensions. The emitted format can be ESM or CommonJS depending on the detected format of each file.

Using Runtime-Compatible Relative Imports

typescript
// src/math.ts
export function add(a: number, b: number): number {
  return a + b;
}

// src/index.ts
import { add } from "./math.js";

console.log(add(10, 20));
    

When compiling Node.js ESM with NodeNext, a .js relative specifier can resolve to a corresponding TypeScript source file while remaining valid for the emitted JavaScript runtime.

Understanding the Difference Between target and module

json
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "CommonJS",
    "strict": true
  }
}
    

This example uses modern JavaScript syntax as its target while selecting CommonJS for module output. Target and module describe different aspects of compilation.

Pro Tip

Treat target, module, and moduleResolution as related but distinct settings. For bundler-based frontend applications, modern module syntax with bundler resolution is often suitable. For Node.js applications, NodeNext can model the runtime’s ESM and CommonJS rules. Always verify package metadata, import extensions, and the production build instead of relying solely on a successful type check.

More TypeScript lessons

🎨 IgKnight Docs

Master HTML, CSS, JS, Python, .NET and more with easy-to-follow lessons and practical code examples.

© 2025 IgKnight Learning Platform. All rights reserved.