如何在TypeScript项目中解决ERR_UNKNOWN_FILE_EXTENSION错误

huangapple go评论112阅读模式
英文:

How to resolve ERR_UNKNOWN_FILE_EXTENSION in TypeScript project

问题

以下是您要翻译的内容:

演示

最近,我尝试设置一个节点项目,其中我使用了ES6的import语句和顶层的await,但我遇到了一个奇怪的问题。

当我使用npx ts-node index.ts来运行我的应用程序时,我发现它不认识我的文件扩展名.ts

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: 未知的文件扩展名“.ts”:/home/zhyp/Code/ts-test/src/index.ts

因此,在进行了一些研究之后,我发现了esm标志(npx ts-node --esm index.ts),在运行这个命令后,实际上找不到我的文件:

throw new ERR_MODULE_NOT_FOUND(
          ^
CustomError: 无法找到模块“/home/zhyp/Code/ts-test/src/imported-file”,它被导入自/home/zhyp/Code/ts-test/src/index.ts

所以经过更多的研究,我尝试将所有导入添加.js扩展名,令人奇怪的是这确实起作用,但如果我将这些文件的扩展名更改为.ts,那么我就需要在我的tsconfig.json中启用allowImportingTsExtensions,并启用notEmit,这将阻止我使用tsc来构建任何内容。

有人可以帮助我理解为什么这个设置不起作用吗?

重现所需的文件:

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 {};
英文:

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 {};

答案1

得分: 1

检查你的 Node 版本。当我从 20.2.0 降级到 19.8.1 时,它对我有效。

英文:

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

答案2

得分: 0

我找到了这个帖子,其中顶部答案帮助我更好地理解为什么这不起作用,使用tsconfig.json上的module: "es2022"package.json上的type: "module"存在一些注意事项。

英文:

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.

huangapple
  • 本文由 发表于 2023年5月30日 03:25:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定