TypeStrong / ts-node

TypeScript execution and REPL for node.js
https://typestrong.org/ts-node
MIT License
12.75k stars 529 forks source link

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" in Node v21.5.0 #2100

Open cibilex opened 6 months ago

cibilex commented 6 months ago

First of all ,I already looked at https://github.com/TypeStrong/ts-node/issues/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.

AaronNGray commented 6 months ago

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.

AaronNGray commented 6 months ago

Heres a working example with node v21.5.0, typescript v5.3.3, ts-node v10.9.2 :- https://github.com/AaronNGray/ts-node-example

NOTE: I have just found this does not seem to support imports !!!

AaronNGray commented 6 months ago

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.

Could you please provide a minimal example of what does not work and then we can try to figure out why !

peschee commented 6 months ago

Heres a working example with node v21.5.0, typescript v5.3.3, ts-node v10.9.2 :- https://github.com/AaronNGray/ts-node-example

This is not an ESM package. Try setting type to module in your package.json.

AaronNGray commented 6 months ago

Heres a working example with node v21.5.0, typescript v5.3.3, ts-node v10.9.2 :- https://github.com/AaronNGray/ts-node-example

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 "type":"module" ? As in :- https://github.com/AaronNGray/ts-node-example/tree/ts-node-esm-module

on v21.5.0 resulting in :-

C:\Users\Nathaniel\test\Node.js\ts-node-example>npm start

> ts-node-example@0.0.1 start
> ts-node src/index.ts

TypeError: Unknown file extension ".ts" for C:\Users\Nathaniel\test\Node.js\ts-node-example\src\index.ts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
    at defaultLoad (node:internal/modules/esm/load:143:22)
    at async ModuleLoader.load (node:internal/modules/esm/loader:409:7)
    at async ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:45)
    at async link (node:internal/modules/esm/module_job:76:21) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
AaronNGray commented 6 months ago

Adding :-

"start:node": "node --loader ts-node/esm src/index.ts"

and tsconfig.json :-

"module": "esnext"

We have :-

https://github.com/AaronNGray/ts-node-example/tree/ts-node-esm-loader

which gives :-

C:\Users\Nathaniel\test\Node.js\ts-node-example>npm run start:node

> ts-node-example@0.0.1 start:node
> node --loader ts-node/esm src/index.ts

(node:18508) 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)
ts-node

NOTE: I have just found this does not seem to support imports !!!

cibilex commented 6 months ago

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.

Could you please provide a minimal example of what does not work and then we can try to figure out why !

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.

AaronNGray commented 6 months ago

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.

Could you please provide a minimal example of what does not work and then we can try to figure out why !

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 : -"start": "ts-node-esm src/index.ts"

The version I posted last works AFAICT without proper testing, but I did do an import

Note: --loader maybe deprecated.

https://github.com/AaronNGray/ts-node-example/tree/ts-node-esm-loader

NOTE: I have just found this does not seem to support imports !!!

AaronNGray commented 6 months ago

@cibilex - Theres a pull request waiting :- https://github.com/TypeStrong/ts-node/pull/2073

cibilex commented 6 months ago

@cibilex - Theres a pull request waiting :- #2073

Thank you a lot,It works now splendid :)

AaronNGray commented 6 months ago

@cibilex - Did you manage to add the two commits to a ts-node fork or did you just patch the diff's ? If you used git I would love to know how ! On the other front I seem to remember GitHub giving diffs but it does not seem to have downloads for them anymore. Or did you just use a reference to the repo in package.json dependencies ?

cj-christoph-gysin commented 6 months ago

@cibilex - Did you manage to add the two commits to a ts-node fork or did you just patch the diff's ? If you used git I would love to know how !

$ git fetch https://github.com/TypeStrong/ts-node refs/pull/2073/head
$ git checkout -b pr-2073 FETCH_HEAD

On the other front I seem to remember GitHub giving diffs but it does not seem to have downloads for them anymore.

You can just add .diff or .patch to any github url for a commit or PR, e.g.: https://github.com/TypeStrong/ts-node/pull/2073.diff

AaronNGray commented 6 months ago

@cibilex - Wow great thanks, think I was over complicating things :)

MickL commented 5 months ago

Guys no need for ts-node, just use Deno or Bun:

deno run main.ts
bun run main.ts
russell-miburn commented 5 months ago

Is there an update on the progress of this bug?

francip commented 4 months ago

To summarize: Looks like this has been there for a while. Adding "type"="module" to package.json breaks ts-node --esm for any node version above v16.

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:

zq0904 commented 4 months ago

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.

Odd-numbered versions of node are typically beta versions and should not be supported

russell-miburn commented 4 months ago

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.

francip commented 4 months ago

Update: The node version where ts-node with package.json "type"="module" breaks is v18.19.0

Simple repro - francip/ts-node-test

djomajeff commented 4 months ago

I use node v20.10.0.

In Package.json apply this configuration:

In tsconfig.json apply the following:

Hope It will work !!

AaronNGray commented 4 months ago

@djomajeff - building a repo following your instructions I am getting :- https://github.com/AaronNGray/ts-node-esm-test/issues/1

I added you to the repo.

AaronNGray commented 4 months ago

@cibilex https://github.com/AaronNGray/ts-node-esm-test works fine now ! I have added a node --test example too.

sleep-written commented 3 months ago

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 .js extension in your imports. For example, check ./src/index.ts:

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 ./sum.js, it tries to resolve it as a ".js" file. But in reality, that file does not exist, and the available file ends with ".ts". To solve the problem, you have several options:

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
francip commented 3 months ago

@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.

sleep-written commented 3 months ago

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:

(node:2036531) 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)

To address this, consider creating a ./ts-node.register.mjs file with:

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 import specifiers:

Relative specifiers like './startup.js' or '../config.mjs'. They refer to a path relative to the location of the importing file. The file extension is always necessary for these.

More details of this problem are explained in this comment. If executing your file with the ./ts-node.register.mjs preloaded results in errors due to relative imports, consider using my module register at @bleed-believer/path-alias. This or building a custom ts-node module register, with the required logic for address relative/absolute imports, could provide a solution too.

francip commented 3 months ago

My example is relevant for node v18.19 and above, where ts-node --esm index.ts command is broken and returns ERR_UNKNOWN_FILE_EXTENSION. (the original problem this issue tracks)

The node --loader ts-node/esm index.ts syntax being deprecated is a separate issue, and the simplest workaround for that is using node --import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("ts-node/esm", pathToFileURL("./"));' index.ts or your library which essentially does very similar thing. And yes, they both will successfully run index.ts, which is why I listed them as potential workarounds.

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.

sleep-written commented 3 months ago

@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 npm run start is the following:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/sleep-written/projects/node.js/ts-node-test/index.ts at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9) at defaultGetFormat (node:internal/modules/esm/get_format:203:36) at defaultLoad (node:internal/modules/esm/load:143:22) at async nextLoad (node:internal/modules/esm/hooks:866:22) at async nextLoad (node:internal/modules/esm/hooks:866:22) at async Hooks.load (node:internal/modules/esm/hooks:449:20) at async handleMessage (node:internal/modules/esm/worker:196:18) { code: 'ERR_UNKNOWN_FILE_EXTENSION' }

However, with npm run start:success works as you suggest. But when I had to update my library to be compatible with node 20, I had to write my own loader to resolve the path aliases. Since I had to write the loader myself, I ran into exactly that problem with the extensions.

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 --import flag.

darcyrush commented 2 months ago

To address this, consider creating a ./ts-node.register.mjs file with:

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

register("ts-node/esm", pathToFileURL("./"));

Thank you all for this, I am able to use ts-node and ESM with my toolset.

I look forward to https://github.com/TypeStrong/ts-node/pull/2073 being merged

AaronNGray commented 2 months ago

@darcyrush - How do you apply this on the command line ?

darcyrush commented 2 months ago

@AaronNGray My use case is very basic;

TS_NODE_PROJECT=./test/tsconfig.json node --import ./ts-node.register.mjs --test-reporter spec --test 'test/**/*.test.ts'

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 tsx briefly which had much better performance, but I came across a bunch of some edge cases and incompatibility issues with other libraries which is why I came back to ts-node.

jm-sky commented 2 months ago

I've spent some time working it out, so I'll share my working config (I'm using typicode/lowdb).

I'm running it with npm run start command

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.ts

import { 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();
narayan639 commented 2 months ago

it solve the problem when i use "type":"commonjs"

DonaldFoss-SCC commented 2 months ago

Guys no need for ts-node, just use Deno or Bun:

deno run main.ts
bun run main.ts

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.

cj-christoph-gysin commented 2 months ago

so that when the execution environment gives you a line number, it is useful information.

Isn't that solved with source maps?

abdullahmehboob20s commented 2 months ago

I removed "type": "module" from package.json and it fixed.

mitch1009 commented 1 month ago

I've spent some time working it out, so I'll share my working config (I'm using typicode/lowdb).

I'm running it with npm run start command

  • NodeJS version node -v => v20.12.2
  • All imports should be relative and have .js extension

    • i.e. import { app } from './src/app.js' (real file name is app.ts)
    • btw. I don't know how to use import aliases like from '@/app.js
  • Import from node with node prefix, like import path from 'node:path'
  • I don't know if everything I put in config files is needed, but it's working
  • I'm using -r dotenv/config in start script to load .env variables, it's optional
  • I'm using --ignore db.json to make nodemon ignore database file changes

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.ts

import { 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();

Thank you for this fix. It works perfectly on my side.

alist commented 1 month ago

Would someone please be able to confirm whether a fix is inbound for node 20.13 and tsnode 10.9, or whether I should be applying one of these workaroundy fixes?

kolya182 commented 3 weeks ago

Somehow, I was able to get this running. I’m not sure what exactly fixed it, but here is my setup. This is so tiresome after returning to programming in TS after a couple of years out, thanks to the articles that helped ([link1]. (https://dev.to/_blank/conquering-the-villains-a-journey-of-setting-up-nodejs-with-express-and-typescript-36ck)). Some pice of code can be redundant !!!

package.json

{
  "scripts": {
    "dev": "nodemon --exec node --no-warnings=ExperimentalWarning --loader ts-node/esm  src/index.ts",
  },
  "dependencies": {
    "axios": "^1.7.2",
  },
  "devDependencies": {
    "nodemon": "^3.1.4",
    "ts-node": "^10.9.2",
    "typescript": "^5.5.2"
  },
  "engines": {
    "node": "22.2.0",
    "npm": "10.8.1"
  },
  "type": "module"
}

tsconfig.json

{
    "compilerOptions": {
        "target": "ESNext",
        "module": "ESNext",
        "allowImportingTsExtensions": true,
        "emitDeclarationOnly": true,
        "declaration": true,
        "strict": true,
        "esModuleInterop": true,                  // Enables emit interoperability between CommonJS and ES Modules via the `__importStar` and `__importDefault` helpers
        "skipLibCheck": true,                     // Skip type checking of declaration files
        "moduleResolution": "Node",
        "forceConsistentCasingInFileNames": true, // Disallow inconsistently-cased references to the same file
        "resolveJsonModule": true                 // Include modules imported with .json extension
      },
      "include": ["src/**/*.ts"],                    // Include files in the src folder
      "exclude": ["node_modules", "dist"],         // Exclude node_modules and dist folders
      "ts-node": {
        "esm": true,
        "compiler": "typescript",
        "experimentalSpecifierResolution": "node"
        }
}

nodemon.json

{
  "watch": ["src"],
  "ext": "ts",
  "execMap": {
    "ts": "ts-node-esm"
  }
}
DzmVasileusky commented 3 weeks ago

I've spent hours if not days trying to make ts-node import my ts files. Then I found the suggestion to use vite-node and it worked like a charm https://github.com/TypeStrong/ts-node/issues/1997#issuecomment-2041467822

rodrigogs commented 3 weeks ago

TypeScript ecosystem is as bad as it gets!

I'm done. I'm moving everything to JS. It doesn't make any sense to try and use a tool that only gives you headaches and prevents you from using libraries.

AaronNGray commented 3 weeks ago

This is no longer an issue and has all been solved plus theres a PR going through then process.

On Tue, 25 Jun 2024 at 22:57, Rodrigo Gomes da Silva < @.***> wrote:

TypeScript ecosystem is as bad as it gets!

I'm done. I'm moving everything to JS. It doesn't make any sense to try and use a tool that only gives you headaches and prevents you from using libraries.

— Reply to this email directly, view it on GitHub https://github.com/TypeStrong/ts-node/issues/2100#issuecomment-2190038874, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAU4EZANEK462XIIPAQ23TZJHRTLAVCNFSM6AAAAABBQ72D62VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOJQGAZTQOBXGQ . You are receiving this because you were mentioned.Message ID: @.***>

-- Aaron Gray - @@.***

Independent Open Source Software Engineer, Computer Language Researcher, Information Theorist, and Computer Scientist.

DzmVasileusky commented 3 weeks ago

@AaronNGray Not sure when it was resolved, I've spent 2 days trying different combinations of configuration and imports with both CommonJs and ESM loaders.

francip commented 3 weeks ago

@AaronNGray What version of ts-node is this resolved in? Is there a new way to invoke ts-node that makes it work? Just tested https://github.com/francip/ts-node-test (which a simple repro for the issue) with node v22.1..0 and here's what I get:

(base) francip francip-laptop ~/src/ts-node-test [git main] $ npm start

> ts-node-test@1.0.0 start
> ts-node --esm index.ts

(node:4668) [DEP0180] DeprecationWarning: fs.Stats constructor is deprecated.
(Use `node --trace-deprecation ...` to show where the warning was created)
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/francip/src/ts-node-test/index.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 nextLoad (node:internal/modules/esm/hooks:750:22)
    at async nextLoad (node:internal/modules/esm/hooks:750:22)
    at async Hooks.load (node:internal/modules/esm/hooks:383:20)
    at async MessagePort.handleMessage (node:internal/modules/esm/worker:199:18) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
AaronNGray commented 3 weeks ago

@francip - here's a working example :-

https://github.com/AaronNGray/node-typescript-and-test-esm-example

It provides a Node.js ESM example using ts-node with an import example src/main.ts and a Node Test Runner example too all using latest npm modules.

Node: v21.7.1 npm: v10.8.1

If you would like to test and verify it I would appreciate it :)

kolya182 commented 3 weeks ago

@francip - here's a working example :-

https://github.com/AaronNGray/node-typescript-and-test-esm-example

It provides a Node.js ESM example using ts-node with an import example src/main.ts and a Node Test Runner example too all using latest npm modules.

Node: v21.7.1 npm: v10.8.1

If you would like to test and verify it I would appreciate it :)

Yehh, but you still import .ts as .js

StreetStrider commented 3 weeks ago

@kolya182 although this looks weird, this is actually most logical way to import, considering ESM requires explicit extension, and TS exists in static time only. Extensionless is not a way anymore, so the only alternative is to have explicit .ts extension instead, which, by ESM rules, means «custom loader». If you introduce custom extention to your stack that is always more hard to setup and it is more error prone. You'll need to learn every part of your stack to understand it. It is an overkill when all you need is just a simple 1-to-1 relation between TS and corresponding JS file.

After a consideration, it turns out that this is a bliss that TS just works in such case. You can easily import .js, no custom resolvers/loaders involved and TS understands it out of the box. And later the code can be easily translated to JS without need to visit and rewrite each import, just stripping types would be enough.

AaronNGray commented 3 weeks ago

@francip - It an engineering solution, it works, is minimally annoying, not perfect, but at the end of the day we can get on and do work, whch is what really matters. niceties aside. Yes I don't like it either and this should have been sorted out properly long ago.

DzmVasileusky commented 3 weeks ago

@AaronNGray Importing .ts as .js extension is super confusing and really not obvious, did you add it to official docs because I doubt anyone will try this really without a hint?

AaronNGray commented 3 weeks ago

Hi @DzmVasileusky no I am hoping there will be a proper solution in the pipeline. This is just an “engineering solution” that will help people out for now so they can get on until a patch that solves this properly comes through. Your welcome to update the docs but it’s also dependent on tsconfig.json settings, and also settings in package.json.

francip commented 3 weeks ago

@AaronNGray I am aware (couple of variations) of the "engineering" solution :-) But as you said it yourself, this is not proper user-friendly solution. When you say

This is no longer an issue and has all been solved

people will tend to believe that the proper solution has been done.