ts-node icon indicating copy to clipboard operation
ts-node copied to clipboard

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

Open luyifo opened this issue 1 year ago • 19 comments

Search Terms

When the node version is 18.20.0, running TS-node --esm **.ts displays TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts". Runs successfully after the Node version is reduced to 16.20.2.

PS D:\Testspace\ts-node-demo> pn dev

> [email protected] dev D:\Testspace\ts-node-demo
> ts-node-esm src/index.ts

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for D:\Testspace\ts-node-demo\src\index.ts
    at new NodeError (node:internal/errors:405:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:136:11)
    at defaultGetFormat (node:internal/modules/esm/get_format:182:36)
    at defaultLoad (node:internal/modules/esm/load:101:20)
    at nextLoad (node:internal/modules/esm/hooks:864:28)
    at load (D:\Testspace\ts-node-demo\node_modules\.pnpm\[email protected]_@[email protected][email protected]\node_modules\ts-node\dist\child\child-loader.js:19:122)
    at nextLoad (node:internal/modules/esm/hooks:864:28)
    at Hooks.load (node:internal/modules/esm/hooks:447:26)
    at MessagePort.handleMessage (node:internal/modules/esm/worker:196:24)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:786:20) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
 ELIFECYCLE  Command failed with exit code 1.
PS D:\Testspace\ts-node-demo> nvm list

  * 18.20.0 (Currently using 64-bit executable)
    16.20.2
PS D:\Testspace\ts-node-demo> nvm use 16.20.2
Now using node v16.20.2 (64-bit)
PS D:\Testspace\ts-node-demo> pn dev

> [email protected] dev D:\Testspace\ts-node-demo
> ts-node-esm src/index.ts

hello world
PS D:\Testspace\ts-node-demo> 

Expected Behavior

Actual Behavior

Steps to reproduce the problem

Minimal reproduction

Specifications

  • ts-node version: 10.9.2
  • node version: 18.20.0
  • TypeScript version: 5.4.5
  • tsconfig.json, if you're using one:
{
  "compilerOptions": {
    "rootDir": "./src",
    "target": "es2016", 
    "module": "NodeNext", 
    "moduleResolution": "NodeNext",
    "esModuleInterop": true, 
    "forceConsistentCasingInFileNames": true, 
    "strict": true, 
    "skipLibCheck": true, 
    "verbatimModuleSyntax": true,
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true
  }
}
  • package.json:
{
  "type": "module",
  "name": "ts-node-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "ts-node-esm src/index.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "ts-node": "^10.9.2",
    "typescript": "^5.4.5"
  }
}
  • Operating system and version: Windows 11 Pro 10.0.22631 Build 22631
  • If Windows, are you using WSL or WSL2?:

luyifo avatar Jun 12 '24 10:06 luyifo

I had the same issue, downgrade Node to 18.18 help. My engines:

"engines": {
    "node": ">=18.18.0 <18.19.0"
  },

etulikov avatar Jun 14 '24 20:06 etulikov

Have you solved this problem?I switched the versions of 22,18 and 16 with different problems, and the configuration file has been modified, but it still doesn't work

EvilCodeMorty avatar Jul 07 '24 11:07 EvilCodeMorty

"module": "CommonJS", "esModuleInterop": true,

EvilCodeMorty avatar Jul 08 '24 16:07 EvilCodeMorty

My code compilation is no problem, I specially configured a file for "ts-node", code compilation is another file, anyway just to see the state of writing code, so I specially configured a file for "ts-node", compilation is another file;"tsconfig.dev.json"and"tsconfig.json"

EvilCodeMorty avatar Jul 08 '24 17:07 EvilCodeMorty

"dev": "cross-env TS_NODE_PROJECT=tsconfig.dev.json nodemon --exec ts-node src/main.ts",

EvilCodeMorty avatar Jul 08 '24 17:07 EvilCodeMorty

"dev": "cross-env TS_NODE_PROJECT=tsconfig.dev.json nodemon --exec ts-node src/main.ts",

EvilCodeMorty avatar Jul 08 '24 17:07 EvilCodeMorty

seems to work for me with node and --loader flag

{
  "scripts": {
-    "not-work": "ts-node-esm src/main.ts",
+    "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning src/main.ts"
  }
}

node: v20.5.0 ts-node: v10.9.2

lhapaipai avatar Jul 17 '24 15:07 lhapaipai

seems to work for me with node and --loader flag

{
  "scripts": {
-    "not-work": "ts-node-esm src/main.ts",
+    "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning src/main.ts"
  }
}

node: v20.5.0 ts-node: v10.9.2

This worked for me. Thanks @lhapaipai

kyledetella avatar Jul 17 '24 20:07 kyledetella

try to use tsx instead. It works flawlessly for me.

meotimdihia avatar Aug 01 '24 08:08 meotimdihia

seems to work for me with node and --loader flag

{
  "scripts": {
-    "not-work": "ts-node-esm src/main.ts",
+    "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning src/main.ts"
  }
}

node: v20.5.0 ts-node: v10.9.2

worked for me too thanks

ranamaniz avatar Aug 12 '24 15:08 ranamaniz

seems to work for me with node and --loader flag

{
  "scripts": {
-    "not-work": "ts-node-esm src/main.ts",
+    "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning src/main.ts"
  }
}

node: v20.5.0 ts-node: v10.9.2

Thanks @lhapaipai!

In my scenario, I had to also workaround the following error. I think it stems from my project does not have a top-level tsconfig.json, but rather sub-folders define different configs similar to a monorepo. So I either had to manually cd down into a sub-folder then run the command, or I could have node figure it out via the import command.

Object.defineProperty(exports, "__esModule", { value: true });
                      ^
ReferenceError: exports is not defined in ES module scope

My modified command is:

{
  "scripts": {
-   "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning src/main.ts"
+   "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning -e \"import('${0}');\""
  }
}

Example Usage:

yarn dev path/to/my/file.ts

douglascayers avatar Sep 14 '24 05:09 douglascayers

seems to work for me with node and --loader flag

{
  "scripts": {
-    "not-work": "ts-node-esm src/main.ts",
+    "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning src/main.ts"
  }
}

node: v20.5.0 ts-node: v10.9.2

Thanks @lhapaipai!

In my scenario, I had to also workaround the following error. I think it stems from my project does not have a top-level tsconfig.json, but rather sub-folders define different configs similar to a monorepo. So I either had to manually cd down into a sub-folder then run the command, or I could have node figure it out via the import command.

Object.defineProperty(exports, "__esModule", { value: true });
                      ^
ReferenceError: exports is not defined in ES module scope

My modified command is:

{
  "scripts": {
-   "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning src/main.ts"
+   "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning -e \"import('${0}');\""
  }
}

Example Usage:

yarn dev path/to/my/file.ts

Awesome, that solved my problem. Thanks!

According to the warnings, I've also added a workaround for the "experimentalWarning" issue. I created a "loader.js" in my project root's folder:

import { register } from "node:module";
import { pathToFileURL } from "node:url";

// Register the TypeScript loader
register("ts-node/esm", pathToFileURL('./'));

Then I changed my package.json to:

{
  "scripts": {
      "dev": "node --import './loader.js' src/index.ts"
  }
}

lhymmEU avatar Oct 18 '24 12:10 lhymmEU

It runs when I change the type to commonjs in package.json.

AmosTmg avatar Oct 22 '24 06:10 AmosTmg

It runs when I change the type to commonjs in package.json.

This breaks things if you are using NPM modules that require ESM.

natesilva avatar Feb 17 '25 19:02 natesilva

seems to work for me with node and --loader flag

{ "scripts": {

  • "not-work": "ts-node-esm src/main.ts",
  • "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning src/main.ts" } } node: v20.5.0 ts-node: v10.9.2

Is this still the best solution? Seems that suppressing these warnings must be a temp solution not fit for production?

atjain02 avatar Feb 18 '25 05:02 atjain02

seems to work for me with node and --loader flag

{ "scripts": {

  • "not-work": "ts-node-esm src/main.ts",
  • "dev": "node --loader ts-node/esm --no-warnings=ExperimentalWarning src/main.ts" } } node: v20.5.0 ts-node: v10.9.2

node: v20.18.1 ts-node: v10.9.2

It's still not working.

node:internal/modules/run_main:128
    triggerUncaughtException(
    ^
[Object: null prototype] {
  [Symbol(nodejs.util.inspect.custom)]: [Function: [nodejs.util.inspect.custom]]
}

Node.js v20.18.1

zurmokeeper avatar Feb 27 '25 02:02 zurmokeeper

Works for me:

node --swc main.ts

mxshdev avatar Mar 25 '25 13:03 mxshdev

Last week ts-node was working without any issues. Now it's giving me headache. Is there an issue with the latest ts-node and typescript or some incompatibility issue with node and ts-node or what is causing this?

Currently the issue can be reproduced by following these steps:

  1. Initialise a new completely empty project a. mkdir test b. pnpm init
  2. pnpm add -D ts-node typescript
  3. touch main.ts
  4. echo "console.log('Hello world')" > main.ts
  5. pnpm exec ts-node main.ts

This will now first throw you an error

(node:62836) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/.../test/main.ts:2
export {};
^^^^^^

SyntaxError: Unexpected token 'export'

Try to fix that by adding the "type": "module" into package.json. Now running pnpm exec ts-node main.ts gives you the very annoying

TypeError: Unknown file extension ".ts" for /Users/.../test/main.ts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:176:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:219:36)
    at defaultLoad (node:internal/modules/esm/load:143:22)
    at async ModuleLoader.load (node:internal/modules/esm/loader:555:7)
    at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:434:45)
    at async link (node:internal/modules/esm/module_job:87:21) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Adding --esm flag does not change anything as the command pnpm exec ts-node --esm main.ts also gives the same error above.

Version used in this test:

  • ts-node - 10.9.2
  • typescript - 5.8.2
  • node - v20.18.0
  • pnpm - 9.13.0

timjam avatar Mar 25 '25 14:03 timjam

@timjam I encountered exactly the same problem, and I'm also using Node.js v20. In my case, I have to create a typescript-hooks.mjs file for each of my projects, and run ts file with node --import=./typescript-hooks.mjs

import { register } from 'node:module';
import { setUncaughtExceptionCaptureCallback } from 'node:process';
import { pathToFileURL } from 'node:url';
setUncaughtExceptionCaptureCallback((err) => {
    console.error(err);
});
register('ts-node/esm', pathToFileURL('./'));

median-dxz avatar Apr 28 '25 09:04 median-dxz