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 yourpackage.json - And make sure your
tsconfig.jsonhas"module": "esnext"or compatible:
{
"type": "module"
}{
"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.
