ts-node never work when type: module is in package.json

156 views Asked by At

This is my project:

.
├── package.json
└── test.ts

This is my package.json file (only):

{
  "type": "module"
}

And this is my test.ts file (only):

console.log("hello world")

When I run

$ npx ts-node ./test.ts

I get the following error:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/$am/Documents/projects/temp/test.ts
    at new NodeError (node:internal/errors:405:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:79:11)
    at defaultGetFormat (node:internal/modules/esm/get_format:124:36)
    at defaultLoad (node:internal/modules/esm/load:84:20)
    at nextLoad (node:internal/modules/esm/loader:163:28)
    at ESMLoader.load (node:internal/modules/esm/loader:603:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:22)
    at new ModuleJob (node:internal/modules/esm/module_job:64:26)
    at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:480:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:434:34) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Is there any solution to this?

  • I am running nodejs v18.17.1.
  • I don't have typescript or ts-node globally installed.
0

There are 0 answers