How to fix Cannot use 'module' option with ts-node without 'esm' in TypeScript

TypeScriptINTERMEDIATEMEDIUMUpdated Dec 11, 2025ยท 120 views

This error occurs when ts-node detects ES module syntax (import/export) in your TypeScript code but ESM support is not enabled. ts-node requires explicit ESM configuration to work with ES modules, which is common in modern TypeScript projects using module: "ESNext" or similar settings.

Quick fix

Fixes Cannot use 'module' option with ts-node without 'esm'

// tsconfig.json
{
  "compilerOptions": {
    "module": "ESNext",  // or "ES2020", "ES2015"
    // ... other options
  }
}
  1. Check your tsconfig.json module setting
  2. Add --esm flag to ts-node command
  3. Set package.json type to "module"
  4. Update ts-node configuration
typescript โ€” error
// tsconfig.json
{
"compilerOptions": {
"module": "ESNext", // or "ES2020", "ES2015"
// ... other options
}
}
Cannot use 'module' option with ts-node without 'esm'

What this error means

The error "Cannot use 'module' option with ts-node without 'esm'" happens when ts-node encounters ES module syntax (import/export statements) in your TypeScript code, but the runtime environment is not configured to support ES modules. ts-node needs to know whether to treat your code as CommonJS or ES modules. When your tsconfig.json specifies a module system like "ESNext", "ES2020", or "ES2015", but you're running ts-node without ESM flags, ts-node cannot properly execute the code. This is a configuration mismatch between your TypeScript compiler settings and your Node.js runtime configuration. TypeScript may be configured to output ES modules, but Node.js (and by extension ts-node) needs explicit instructions to load ES modules rather than the default CommonJS modules.

Typical symptoms

  • ts-node fails with error: "Cannot use 'module' option with ts-node without 'esm'"
  • TypeScript files with import/export statements fail to execute
  • The error occurs even with correct tsconfig.json settings
  • Running the same code with tsc compilation works, but ts-node execution fails
  • Project uses modern TypeScript configuration with module: "ESNext" or similar

Common causes

  1. tsconfig.json has "module" set to an ES module value (ESNext, ES2020, ES2015) but ts-node is running without ESM support
  2. Missing --esm flag when running ts-node with ES module configuration
  3. package.json missing "type": "module" field when using ES modules
  4. Using import/export syntax in TypeScript files without proper ESM configuration
  5. Running ts-node via npx or script without necessary ESM flags

How to fix "Cannot use 'module' option with ts-node without 'esm'"

1Check your tsconfig.json module setting

First, verify what module system your TypeScript is configured to use:

json
// tsconfig.json
{
  "compilerOptions": {
    "module": "ESNext",  // or "ES2020", "ES2015"
    // ... other options
  }
}

If your module is set to an ES module value (ESNext, ES2020, ES2015, etc.), you need ESM support.

2Add --esm flag to ts-node command

When running ts-node with ES module configuration, add the --esm flag:

bash
# For direct execution
npx ts-node --esm your-file.ts

# For ts-node/register
node --loader ts-node/esm --experimental-specifier-resolution=node your-file.ts

This tells ts-node to enable ES module support.

3Set package.json type to "module"

If your project uses ES modules, add this to your package.json:

json
{
  "type": "module",
  "scripts": {
    "start": "ts-node --esm src/index.ts"
  }
}

This tells Node.js to treat .js files as ES modules by default.

4Update ts-node configuration

You can configure ts-node to always use ESM by creating a tsconfig.json for ts-node:

json
// tsconfig.json
{
  "ts-node": {
    "esm": true,
    "experimentalSpecifierResolution": "node"
  },
  "compilerOptions": {
    "module": "ESNext"
  }
}

Or use a ts-node configuration file:

json
// .ts-noderc.json
{
  "esm": true,
  "experimentalSpecifierResolution": "node"
}
5Use alternative execution methods

If you continue to have issues, try alternative approaches:

1. Use tsx instead of ts-node:

bash
npx tsx your-file.ts

2. Compile first, then run:

bash
npx tsc
node dist/index.js

3. Use Node.js with ts-node/esm loader:

bash
node --loader ts-node/esm --experimental-specifier-resolution=node your-file.ts
6Verify Node.js version compatibility

Ensure your Node.js version supports ES modules:

- Node.js 12.17.0+ for experimental ES modules
- Node.js 14.13.0+ for stable ES module support
- Node.js 16.0.0+ for improved ES module features

Check your version:

bash
node --version

Update if necessary:

bash
# Using nvm
nvm install --lts
nvm use --lts

Advanced notes

Understanding the module systems:

- CommonJS: Node.js's original module system using require() and module.exports
- ES Modules (ESM): JavaScript's standard module system using import and export

Why this happens: TypeScript can compile to either module system. When you set "module": "ESNext" in tsconfig.json, TypeScript outputs ES module syntax. However, Node.js defaults to CommonJS, so ts-node needs explicit configuration to handle ES modules.

Performance considerations: ESM loading can be slightly slower than CommonJS due to asynchronous nature. For development, the difference is negligible, but for production, consider pre-compilation.

Migration path: If migrating from CommonJS to ESM:
1. Update package.json with "type": "module"
2. Change file extensions from .js to .mjs or use package.json type
3. Update all import/export statements
4. Configure ts-node with --esm flag

Common pitfalls:
- Mixed module systems in the same project
- Third-party packages that don't support ESM
- Different behavior between development (ts-node) and production (compiled)

Frequently asked questions

What does "Cannot use 'module' option with ts-node without 'esm'" mean?
The error "Cannot use 'module' option with ts-node without 'esm'" happens when ts-node encounters ES module syntax (import/export statements) in your TypeScript code, but the runtime environment is not configured to support ES modules. ts-node needs to know whether to treat your code as CommonJS or ES modules. json specifies a module system like "ESNext", "ES2020", or "ES2015", but you're running ts-node without ESM flags, ts-node cannot properly execute the code.
How do I fix "Cannot use 'module' option with ts-node without 'esm'"?
This error occurs when ts-node detects ES module syntax (import/export) in your TypeScript code but ESM support is not enabled. ts-node requires explicit ESM configuration to work with ES modules, which is common in modern TypeScript projects using module: "ESNext" or similar settings.
What causes "Cannot use 'module' option with ts-node without 'esm'"?
tsconfig.json has "module" set to an ES module value (ESNext, ES2020, ES2015) but ts-node is running without ESM support Missing --esm flag when running ts-node with ES module configuration package.json missing "type": "module" field when using ES modules
Is the "Cannot use 'module' option with ts-node without 'esm'" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading