4

I'm trying to run a single ES module .ts file in a Next.js project with default config for quick debugging:

npx ts-node lib/my_module.ts

which gives me this error:

Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

I wouldn't want to mess with the default config so looking for a sane way to make this work.

mirkokiefer
  • 3,347
  • 4
  • 20
  • 25

1 Answers1

11

It turns out there is a ts-node specific config section we can add to tsconfig.json.

This won't interfere with the Next.js compiler options.

  ...
  "ts-node": {
    "compilerOptions": {
      "module": "CommonJS"
    }
  },
  ...

See also https://github.com/TypeStrong/ts-node/issues/935#issuecomment-913179458.

After this you can simply run a file with ts-node lib/my_module.ts.

mirkokiefer
  • 3,347
  • 4
  • 20
  • 25