TS1286

ESM syntax is not allowed in a CommonJS module when 'verbatimModuleSyntax' is enabled.

Broken Code ❌

// In a file treated as CommonJS (e.g. `.ts` with no `"type": "module"` in package.json)
import { readFileSync } from 'node:fs';

This error happens when your file is being compiled as a CommonJS module, but you are using ESM import/export syntax with the TypeScript option "verbatimModuleSyntax": true.

This setting means: "Do not transform or remove any imports or exports that aren’t marked as type-only", so your ESM syntax is preserved exactly as written. If the output target is CommonJS, this leads to incompatible code.

Fixed Code βœ”οΈ

Use CommonJS-style imports

To fix the issue you have to use CommonJS-style imports in your CommonJS module:

const { readFileSync } = require('node:fs');

Convert your project to ESM

  • Change your file extension to .mts
  • Or add "type": "module" in your package.json
  • And make sure your tsconfig.json has "module": "esnext" or compatible:
package.json
{
  "type": "module"
}
tsconfig.json
{
  "compilerOptions": {
    "module": "esnext",
    "verbatimModuleSyntax": true
  }
}

When "verbatimModuleSyntax" is enabled, TypeScript won’t transform your import/export syntax. That means you're responsible for making sure your module system (CommonJS or ESM) matches your syntax. Use CommonJS-style require in CommonJS projects or configure your project for ESM to keep using import.