英文:
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 {};
英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论