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
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" in Node v21.5.0 #2100
Comments
Getting same issue, but I have a working version that I solved this issue on, and now a new project on which it does not. It is something else and I cannot remember what it is. Will report back once I have debugged the issue. |
Heres a working example with node v21.5.0, typescript v5.3.3, ts-node v10.9.2 :- NOTE: I have just found this does not seem to support imports !!! |
Could you please provide a minimal example of what does not work and then we can try to figure out why ! |
This is not an ESM package. Try setting type to module in your package.json. |
AFAICS I am not sure ts-node supports ESM. Could you provide the example that works on node v19.0.0, but not v21.5.0. Or is it as simple as setting on v21.5.0 resulting in :-
|
Adding :-
and tsconfig.json :-
We have :- https://github.com/AaronNGray/ts-node-example/tree/ts-node-esm-loader which gives :-
NOTE: I have just found this does not seem to support imports !!! |
I've modified the project a bit and created new repository.link: https://github.com/cibilex/ts-node-err This project doesn't work in node v21.5.0 while works In node v16.20.2 for me. |
You modified start to : - The version I posted last works AFAICT without proper testing, but I did do an Note: https://github.com/AaronNGray/ts-node-example/tree/ts-node-esm-loader NOTE: I have just found this does not seem to support imports !!! |
@cibilex - Did you manage to add the two commits to a ts-node fork or did you just patch the diff's ? |
You can just add |
@cibilex - Wow great thanks, think I was over complicating things :) |
Guys no need for
|
Is there an update on the progress of this bug? |
To summarize: Looks like this has been there for a while. Adding The workarounds I've found so far:
If you are using webpack in your project, this also breaks using Typescript config files. The only workaround for this are:
|
Odd-numbered versions of node are typically beta versions and should not be supported |
I realised that the issue that I had was not with the version of node or ts-node but I was importing a typescript dependancy incorrectly from a library. removing the dependency i was importing and importing the .js from the library rather that a typescript file fixed this issues. |
Update: The node version where ts-node with package.json Simple repro - francip/ts-node-test |
I use node v20.10.0. In Package.json apply this configuration:
In tsconfig.json apply the following:
Hope It will work !! |
@djomajeff - building a repo following your instructions I am getting :- AaronNGray/ts-node-esm-test#1 I added you to the repo. |
@cibilex https://github.com/AaronNGray/ts-node-esm-test works fine now ! |
One of the reasons for the failures with ts-node in ESM projects is linked to the file extensions in import statements. To understand the problem, let's envision this directory structure: .
├── node_modules
├── src
│ ├── sum.ts
│ └── index.ts
├── package.json
├── package-lock.json
└── tsconfig.json In ESM, you must use the import { sum } from './sum.js'; // ...but in reality, the actual file ends in ".ts"
const r = sum(55, 66);
console.log('value:', r); Thus, when the original ts-node loader receives that import
I have created a module called @bleed-believer/path-alias (with ts-node as a dependency). Initially created to resolve path aliases, it now also solves this issue. To execute TypeScript files in ESM directly in Node 20 and above: # Install the library:
npm i --save @bleed-believer/path-alias
# Execute your code using the command line:
npx bb-path-alias ./src/index.ts
# ...or if you need to execute using the node executable:
node --import @bleed-believer/path-alias ./src/index.ts |
@sleep-written Both ECMA 2025 and ECMA 2020 do not specify explicit extension, and the only import examples I found in both ( ECMA 2025, ECMA 2020 ) do not include any extension. But that's neither here, nor there... If you look at my example, you will see nowhere do I use import statement, and the failure happens with a single file project. This is a basic scenario where ts-node should work without extra intermediaries. Though, for folks who are just looking for a quick workaround, your solution might be good. |
Hi @francip, your example is relevant for Node.js versions 18 and earlier. However, when using Node.js version 20 or above with the command: node --loader ts-node/esm ./src/index.ts ...you might encounter the following output:
To address this, consider creating a import { pathToFileURL } from "node:url";
import { register } from "node:module";
register("ts-node/esm", pathToFileURL("./")); And then launch your application with: node --import ./ts-node.register.mjs ./src/index.ts This approach should seamlessly solve the issue, unless your project uses relative imports. According to the Node.js
More details of this problem are explained in this comment. |
My example is relevant for node v18.19 and above, where The However, this workaround does not help people who want to use ESM, webpack, and TS for their webpack config, as webpack internally calls ts-node, thus hitting the same issue. |
@francip i made a fork of your example, adding a function: .
├── README.md
├── another-function.ts # this file.
├── index.ts
├── package-lock.json
├── package.json
└── tsconfig.json ...and importing that funcion using the ESM syntax according to the node.js documentation: import { anotherFunction } from './another-function.js'; // Here
export function hello(who: string = "world"): string {
return `Hello ${who}! `;
}
console.log(hello("ts-node"));
anotherFunction(); The result of execute
However, with In conclusion, as you say, if you only want to make ts-node able to execute typescript code, you would simply have to implement a module that is preloaded with the |
Thank you all for this, I am able to use ts-node and ESM with my toolset. I look forward to #2073 being merged |
@darcyrush - How do you apply this on the command line ? |
@AaronNGray My use case is very basic;
I use that with Node 21 and above for the native test suite functionality. Test suite execution performance is very slow however. I'm not sure if that's a nodejs issue or a ts-node issue. I used |
I've spent some time working it out, so I'll share my working config (I'm using I'm running it with
package.json{
"name": "ts-app",
"exports": "./index.js",
"type": "module",
"scripts": {
"start": "nodemon --exec node --no-warnings=ExperimentalWarning --loader ts-node/esm -r dotenv/config --ignore db.json ./index.ts"
},
"engines": {
"node": ">=16"
},
"dependencies": {
"@types/node": "^20.12.8",
"dotenv": "^16.4.5",
"lowdb": "^7.0.1",
"nodemon": "^3.1.0",
"ts-node": "^10.9.2"
}
} tsconfig.json{
"compilerOptions": {
"baseUrl": ".",
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
"lib": ["ESNext"],
"noPropertyAccessFromIndexSignature": false,
"isolatedModules": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"typeRoots": ["./src/types/global.d.ts", "./node_modules/@types"],
},
"include": ["./index.ts", "./src/*"],
"exclude": ["./db.json"],
"ts-node": {
"esm": true,
"files": true,
"transpileOnly": true,
"experimentalResolver": true,
"experimentalSpecifierResolution": "node"
}
} src/db.tsimport { LowSync } from 'lowdb';
import { JSONFileSyncPreset } from 'lowdb/node';
import { DbSchema } from './types/index.js';
const defaultData: DbSchema = {
messages: []
};
class Db {
db: LowSync<DbSchema>;
constructor() {
this.db = JSONFileSyncPreset<DbSchema>('db.json', defaultData);
}
}
export const db = new Db(); |
it solve the problem when i use "type":"commonjs" |
I wish I could use Bun. It just makes sense to run TS directly so that when the execution environment gives you a line number, it is useful information. The problem in my current project is that Bun doesn't support mssql database driver because of net::socket, streams, and duplex. Deno is very opinionated and requires lots of code changes to get it to work. If Bun didn't feel like it gave me trouble with something every time I tried it, I'd be using it. When it works, it works great. When it doesn't, it's a pain. FWIW, I'm using node 22, ts-node 10.9.2, typescript 5.4.5, package.json:type module, tsconfig.json:module esnext, moduleResolution: node, esModuleInterop: true, target es2022, and ts-node:esm: true. |
Isn't that solved with source maps? |
First of all ,I already looked at #1997 and most of other websites to solve the problem but I couldn't.This error is throwed when I try to use in Node v21.5.0.I realized that The problem is node version,when I updated Node version to v19.0.0 it works.Could you please update ts-node to work in new Node versions.
Best regards.
The text was updated successfully, but these errors were encountered: