1

Demo

Recently I tried setting up a node project where I use ES6 import statements and top-level await, and I came across a weird problem.

While using npx ts-node index.ts to run my application, I found that it didn't know my file extension .ts:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/zhyp/Code/ts-test/src/index.ts

So after some research, I came across the esm flag (npx ts-node --esm index.ts), after running with this command, my files actually could not be found:

throw new ERR_MODULE_NOT_FOUND(
          ^
CustomError: Cannot find module '/home/zhyp/Code/ts-test/src/imported-file' imported from /home/zhyp/Code/ts-test/src/index.ts

So after more research, I've tried adding the .js extension to all imports, which weirdly enough worked, but not if I changed those files' extensions to .ts, because then I would need to enabled allowImportingTsExtensions in my tsconfig.json, and enable notEmit that would prevent me from using tsc to build anything.

Can anyone help me understand why exactly this setup won't work?

Necessary files to reproduce:

tsconfig.json

{
  "compilerOptions": {
    "target": "es2022",
    "module": "es2022",
    "rootDir": "src",
    "resolveJsonModule": true,
    "allowJs": true,
    "outDir": "build",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitAny": true,
    "skipLibCheck": true
  }
}

package.json

{
  "name": "typescript-node",
  "version": "1.0.0",
  "description": "TypeScript Template with Node.js",
  "main": "src/index.js",
  "scripts": {
    "start": "nodemon --exec 'ts-node' src/index.ts"
  },
  "dependencies": {
    "@types/node": "14.14.29",
    "ts-node": "10.9.1",
    "typescript": "5.0.4"
  },
  "devDependencies": {
    "@types/express": "^4.17.6",
    "nodemon": "1.18.4"
  },
  "keywords": [],
  "type": "module"
}

index.ts

import test from "./imported-file";

console.log(test);

imported-file.ts

export default {};
zhyp
  • 187
  • 3
  • 15
  • 3
    Please give a [mre]. If you don't set any experimental flags you have to use `.js` extension on imports even though the source files are .ts, yes. – jonrsharpe May 29 '23 at 19:31
  • I've added a minimal reproducible demo, could you check it out? Also, I'm not sure if you are talking about target and module `es2022` about being experimental, or the `esm`, if it's the latter, I couldn't make it work without it, as you can see on the demo, I'm not using `esm`, but it's not working, if you have a solution for it, I would appreciate. – zhyp May 29 '23 at 20:20
  • 1
    That's an off-site link, which isn't the same thing. And by flags I mean e.g. https://nodejs.org/docs/latest-v18.x/api/cli.html#--experimental-specifier-resolutionmode – jonrsharpe May 29 '23 at 20:22
  • @jonrsharpe, can you check again. – zhyp May 29 '23 at 20:30
  • 1
    Would you [edit] the title to be more indicative of your issue (something like "How to resolve ERR_UNKNOWN_FILE_EXTENSION in TypeScript project"), or would you mind if I do? And please add something about your actual error in the question itself (saying "I found that it didn't know my file extension .ts" is paraphrasing what's going on, which is difficult to search for or reproduce) And then is this a duplicate of [this question](https://stackoverflow.com/q/62096269/2887218)? – jcalz May 29 '23 at 20:31
  • 1
    @jcalz I came across that question, it did not solve my problem, I wanted to use `import` if possible and adding `esModuleInterop` to my `compilerOptions` did not help. I would like to know if something in this setup in causing this problem. – zhyp May 29 '23 at 20:35
  • 1
    You should probably [edit] the question to describe your problem in detail (exact text of errors and where they occur) and link to any similar questions you found in your research and why they are not applicable to your problem (so that nobody closes your question as a duplicate prematurely). I don't think I have any particular insight here so I'll disengage; good luck! – jcalz May 29 '23 at 20:39
  • 1
    Just edited it, do you think I need anything else? @jcalz – zhyp May 29 '23 at 20:41
  • 1
    Well, you might want to provide links to other questions which look similar but don't answer your question, to distinguish your situation from them. But that's your call. – jcalz May 29 '23 at 20:43

2 Answers2

1

Check you node version. When i downgraded from 20.2.0 to 19.8.1, it worked for me

Rishfilet
  • 61
  • 5
  • I'm using Node.js 18 (LTS), I'm not 100% sure, but I think everything set up in the config file should be supported by Node 18 – zhyp May 31 '23 at 11:49
0

By accident, I found this post, where the top answer helped me understand better on why this was not working, there are some caveats on using module: "es2022" on tsconfig.json and type: "module" on package.json.

zhyp
  • 187
  • 3
  • 15