Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERR_UNKNOWN_FILE_EXTENSION on Node v20.0.0 #1997

Open
septs opened this issue Apr 20, 2023 · 115 comments
Open

ERR_UNKNOWN_FILE_EXTENSION on Node v20.0.0 #1997

septs opened this issue Apr 20, 2023 · 115 comments

Comments

@septs
Copy link

septs commented Apr 20, 2023

Search Terms

Node, ERR_UNKNOWN_FILE_EXTENSION

Expected Behavior

Fix it

Actual Behavior

see Minimal reproduction

Steps to reproduce the problem

see Minimal reproduction

Minimal reproduction

$ cat example.ts
console.log('example')
$ cat tsconfig.json
{ "ts-node": { "esm": true } }
$ npx ts-node example.ts
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/septs/Projects/example/example.ts
    at new NodeError (node:internal/errors:399:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:99:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:139:38)
    at defaultLoad (node:internal/modules/esm/load:83:20)
    at nextLoad (node:internal/modules/esm/hooks:781:28)
    at load (/Users/septs/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/dist/child/child-loader.js:19:122)
    at nextLoad (node:internal/modules/esm/hooks:781:28)
    at Hooks.load (node:internal/modules/esm/hooks:381:26)
    at handleMessage (node:internal/modules/esm/worker:153:24)
    at checkForMessages (node:internal/modules/esm/worker:102:28) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Specifications

  • ts-node version: 10.9.1

  • node version: 20.0.0

  • TypeScript version: 5.0

  • tsconfig.json:

    { "ts-node": { "esm": true } }
  • package.json:

    {}
  • Operating system and version: macOS 13.3.1

@septs
Copy link
Author

septs commented Apr 20, 2023

microsoft/TypeScript#53922


Edited by 11/18/2023

Use tsx replace ts-node?

Or use ts-node@beta?

more see https://github.com/privatenumber/tsx, #2077

@csvn
Copy link

csvn commented Apr 20, 2023

I have the same issue. I tried using --esm, { "esm": true } and --loader="ts-node/register", but got the ERR_UNKNOWN_FILE_EXTENSION no matter what I tried. Could not get ts-node to run on Node v20 with native ESM modules.

@Jack-Works
Copy link

FYI Node 20 has made a breaking change:

Custom ESM loader hooks run on dedicated thread
ESM hooks supplied via loaders (--experimental-loader=foo.mjs) now run in a dedicated thread, isolated from the main thread. This provides a separate scope for loaders and ensures no cross-contamination between loaders and application code.

https://nodejs.org/en/blog/release/v20.0.0

@RobinTail
Copy link

+1

The issue also appears when using the specially dedicated ESM runner

$ ts-node-esm file.ts

queengooborg added a commit to openwebdocs/mdn-bcd-collector that referenced this issue Apr 22, 2023
@manuth
Copy link

manuth commented Apr 22, 2023

I went forward and downgraded my node version to lts.
However, doing node --loader ts-node/esm {file} did work for me.

@wibed
Copy link

wibed commented Apr 23, 2023

i expirience the same issue on:

# node --version
v18.16.0

EDIT:
my bad, i misinterpreted the gravity of the situation. manually enforcing the loader like this.

{
  "start": "npm run clean && cross-env NODE_ENV=development NODE_OPTIONS=\"--loader=ts-node/esm --trace-warnings\" webpack serve --mode=development --config webpack.config.ts",
}

worked out.

example sourced from here:
webpack/webpack-cli#2458

@RandivCosta
Copy link

i expirience the same issue on: Node v20,
I tried: $ node --loader ts-node/esm ./path
It worked for me but getting this warnning:
ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time

@sywesk
Copy link

sywesk commented May 4, 2023

I'd like to add quickly that on windows (at least), doing node --loader ts-node/esm ./path works but with an insanely high CPU usage. Doing a quick npx tsc + node ./build/app.js works perfectly without the performance issue.

@RobinTail
Copy link

RobinTail commented May 6, 2023

Yes, node --no-warnings=ExperimentalWarning --loader ts-node/esm file.ts helps to bypass the issue until it's fixed.

@Aenigma
Copy link

Aenigma commented May 6, 2023

yeah, I recently upgraded to node 20 and started running with the --loader ts-node/esm parameter but noticed that my memory usage in my application had skyrocketed to 32G before it would die from OOM. I went chasing for a cause because I assumed it was due to a change I made, but downgrading to node 19 lowered memory usage down to around 500M again. I assume this is probably related to the CPU issues @sywesk had.

So I'd recommend people stay off Node 20 until this is fixed if you don't want to have unexpected issues in your runtime.

bencoveney added a commit to bencoveney/bencoveney.github.io that referenced this issue Mar 30, 2024
ebabani added a commit to Open-Cap-Table-Coalition/OCF-XState-Validation that referenced this issue Apr 2, 2024
The current start command fails with node 20 or node 18.9.
This seems to be an issue related to the node --loader flag: TypeStrong/ts-node#1997

Was able to get around it by calling ts-node directly on index.ts and run the validators
@phosmium
Copy link

phosmium commented Apr 2, 2024

This is horrible. It's been such a long time now and this is still unfixed. @blakeembrey

Saneri added a commit to Saneri/event-signup that referenced this issue Apr 4, 2024
Had problems with ts-node on Node 20 so needed to use something else TypeStrong/ts-node#1997
xuhdev added a commit to xuhdev/typedoc-site that referenced this issue Apr 6, 2024
Since ESM loader hookers now run on a dedicated thread, load the esm
module from node directly:

TypeStrong/ts-node#1997 (comment)
xuhdev added a commit to xuhdev/typedoc-site that referenced this issue Apr 6, 2024
Since ESM loader hookers now run on a dedicated thread, load the esm
module from node directly:

TypeStrong/ts-node#1997 (comment)
xuhdev added a commit to xuhdev/typedoc-site that referenced this issue Apr 6, 2024
Since ESM loader hookers now run on a dedicated thread, load the esm
module from node directly:

TypeStrong/ts-node#1997 (comment)
@PrimalZed
Copy link

My solution was to use vite-node instead.

@RobinTail
Copy link

I'd recommend tsx as alternative, @phosmium && @PrimalZed .
In my experience it works faster than vite-node and it has flawless ESM support right out of the box without any crutches comparing to swc-node.

@felipecrs
Copy link

On the other hand, tsx lacks type checking. tsimp may be a better drop-in replacement than tsx.

jkomoros added a commit to jkomoros/card-web that referenced this issue Apr 8, 2024
…te:config.

This likely broke when upgrading node at some point.

Found a suggestion on TypeStrong/ts-node#1997 to just use tsx, which works! Part of #688.
@jcapogna
Copy link

jcapogna commented Apr 8, 2024

You can do with just node. Instead of node, I'm doing this:

node --no-warnings=ExperimentalWarning --loader ts-node/esm

@DASPRiD
Copy link

DASPRiD commented Apr 8, 2024

On the other hand, tsx lacks type checking. tsimp may be a better drop-in replacement than tsx.

So does ts-node when used with swc, which you really want during development. Time-consuming type checking can be done during build and in general in your CI process. Plus your IDE does type checking as well.

@RobinTail
Copy link

On the other hand, tsx lacks type checking.

you can do tsc --noEmit for checking types before execution, but I find types checking more suitable for the testing scope, not for execution of something, @felipecrs

@felipecrs
Copy link

felipecrs commented Apr 8, 2024

Regardless of preferences, ts-node type checks by default and therefore tsimp is a better drop-in replacement to ts-node than tsx since it behaves the same as ts-node by default (I'm not taking into account when used with swc).

If you don't need type checking, then go with tsx of course.

But, for example, my use case needs type checking when executing the application.

@carlos-menezes
Copy link

tsx also offers no support for emitDecoratorMetadata, which doesn't bide well for any libraries that need reflect-metadata (e.g. TypeORM).

@Lodjuret2001
Copy link

I got my "npm run start" script to work with nodemon this way if this helps anyone:

"start": "nodemon --exec node --no-warnings=ExperimentalWarning --loader ts-node/esm src/server.ts"

Replace "src/server.ts" with the path to the file you want to execute.

@mausam-digital
Copy link

My solution was to use vite-node instead.

Awesome this was the easiest workaround, instead of reading all of this useless technical mumbo-jumbo discussion.

@Jamyth
Copy link

Jamyth commented Apr 26, 2024

How can I specify the tsconfig when using

node --loader ts-node/esm ./myscript.mts

@felipecrs
Copy link

This issue may be a good excuse to try Deno or Bun instead, btw.

@basselalsayed
Copy link

In the end I went with tsx, 0 config and having a separate typecheck command is what I was used to before so not a big deal

frontbest0726 added a commit to frontbest0726/shadcn-ui that referenced this issue Apr 26, 2024
…#1977)

This pull request resolves #1926 and prevents issues like it from happening in the future

## Rationale for this PR

This PR changes the TypeScript execution package for use in scripts like `build:registry` from `ts-node` to `tsx`. This is because `ts-node` has many difficult quirks to work through (and is slow). In addition, it also has a difficult to understand error for newcomers that *is* reproducible.

### The ts-node error

As shown in #1926, using `ts-node` (specifically in `build:registry`) results in this error: `Unknown file extension ".ts" for /ui/apps/www/scripts/build-registry.ts`. There are many issues in the `ts-node` repository documenting this problem:
* TypeStrong/ts-node/issues/1062
* TypeStrong/ts-node/issues/2033
* TypeStrong/ts-node/issues/1997

Switching the typescript-in-node system to `tsx`, which uses esbuild under the hood, resolves this error.

This PR shouldn't affect tests, representation, etc. and is merely a change of build tools. There is no urgent need to merge this.

I accidentally deleted the head repository on #1937. That will not happen again.
ShinyFrontDev added a commit to ShinyFrontDev/Radix-ui-Tailwind-CSS that referenced this issue May 4, 2024
…#1977)

This pull request resolves #1926 and prevents issues like it from happening in the future

## Rationale for this PR

This PR changes the TypeScript execution package for use in scripts like `build:registry` from `ts-node` to `tsx`. This is because `ts-node` has many difficult quirks to work through (and is slow). In addition, it also has a difficult to understand error for newcomers that *is* reproducible.

### The ts-node error

As shown in #1926, using `ts-node` (specifically in `build:registry`) results in this error: `Unknown file extension ".ts" for /ui/apps/www/scripts/build-registry.ts`. There are many issues in the `ts-node` repository documenting this problem:
* TypeStrong/ts-node/issues/1062
* TypeStrong/ts-node/issues/2033
* TypeStrong/ts-node/issues/1997

Switching the typescript-in-node system to `tsx`, which uses esbuild under the hood, resolves this error.

This PR shouldn't affect tests, representation, etc. and is merely a change of build tools. There is no urgent need to merge this.

I accidentally deleted the head repository on #1937. That will not happen again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests