Node 20.9.0 LTS: How to run TypeScript with ts-node with esm support?

2.7k views Asked by At

I have upgraded my nodejs version from 18 to 20.9.0 LTS. I have a project with typescript, express and node. These are the package.json:

{
  "name": "myproject",
  "version": "1.0.0",
  "private": "true",
  "license": "UNLICENSED",
  "main": "dist/index.js",
  "scripts": {
    "dev": "NODE_ENV=development nodemon",
    "dev-lite": "NODE_ENV=development node --loader ts-node/esm src/index.ts",
  },
  "dependencies": {
   
  },
  "devDependencies": {
   
  }
}

This is my tsconfig:

{
  "compilerOptions": {
    "module": "CommonJS",
    "esModuleInterop": true,
    "target": "ES6",
    "noImplicitAny": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "strict": true,
    "paths": {
      "*": [
        "node_modules/*"
      ]
    }
  },
  "include": [
    "src/**/*"
  ]
}

After upgrading to Node.js 20.9.0, when I run yarn dev lite, I get the following error:

yarn run v1.22.19
$ NODE_ENV=development node --loader ts-node/esm src/index.ts
(node:12820) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));'
(Use `node --trace-warnings ...` to show where the warning was created)
(node:12820) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(node:12820) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
/home/mumin/welcome_localdev/sc-graphql-store-writer/src/index.ts:1
import app from './app';
^^^^^^

SyntaxError: Cannot use import statement outside a module
  at internalCompileFunction (node:internal/vm:73:18)
  at wrapSafe (node:internal/modules/cjs/loader:1153:20)
  at Module._compile (node:internal/modules/cjs/loader:1205:27)
  at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
  at Module.load (node:internal/modules/cjs/loader:1091:32)
  at Module._load (node:internal/modules/cjs/loader:938:12)
  at cjsLoader (node:internal/modules/esm/translators:284:17)
  at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:234:7)
  at ModuleJob.run (node:internal/modules/esm/module_job:217:25)
  at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)

Node.js v20.9.0
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I have added type: "module" to config.json. Then I am getting this:

yarn run v1.22.19
$ NODE_ENV=development node --loader ts-node/esm src/index.ts
(node:21831) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));'
(Use `node --trace-warnings ...` to show where the warning was created)
file:///home/mumin/welcome_localdev/sc-graphql-store-writer/src/index.ts:5
Object.defineProperty(exports, "__esModule", { value: true });
                      ^

ReferenceError: exports is not defined in ES module scope
    at file:///home/mumin/welcome_localdev/sc-graphql-store-writer/src/index.ts:5:23
    at ModuleJob.run (node:internal/modules/esm/module_job:217:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:66:12)

Node.js v20.9.0
error Command failed with exit code 1.

I have also changed the "module: Node16" and "module: ESNext" in tsconfig.json. After that I got the following error:

yarn run v1.22.19
$ NODE_ENV=development node --loader ts-node/esm src/index.ts
(node:28358) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));'
(Use `node --trace-warnings ...` to show where the warning was created)
file:///home/mumin/welcome_localdev/sc-graphql-store-writer/src/index.ts:5
Object.defineProperty(exports, "__esModule", { value: true });
                      ^

ReferenceError: exports is not defined in ES module scope
    at file:///home/mumin/welcome_localdev/sc-graphql-store-writer/src/index.ts:5:23
    at ModuleJob.run (node:internal/modules/esm/module_job:217:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:66:12)

Node.js v20.9.0
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Could not find any way to solve the issue. Any recommendation?

1

There are 1 answers

1
Dimava On

You cannot use "module": "CommonJS" with type: "module", use es2022 or node16 (see autocompletion)