繁体   English   中英

ts-node TypeError [ERR_UNKNOWN_FILE_EXTENSION]:未知文件扩展名“.ts”

[英]ts-node TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

我正在使用ts-node但它给了我这个错误:

$ ts-node index.ts

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/projects/node-hddds8/index.ts

我试图从我的package.json中删除"type": "module" ,但在这种情况下,我得到一个不同的错误:

$ ts-node index.ts

(node:45) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/projects/node-hddds8/index.ts:1
import chalk from 'chalk';
^^^^^^

SyntaxError: Cannot use import statement outside a module

这是 StackBlitz 上的复制链接: https://stackblitz.com/edit/node-hddds8?file=index.ts

我的package.json看起来像这样:

{
  "name": "node-starter",
  "version": "0.0.0",
  "type": "module",
  "dependencies": {
    "chalk": "^5.0.1",
    "ts-node": "^10.8.1",
    "typescript": "^4.7.4"
  }
}

我的tsconfig.json看起来像这样:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "Node",
  }
}

我的index.ts看起来像这样:

import chalk from 'chalk';

console.log(chalk.blue('Hello world!'));

我遇到过同样的问题。 降级到v4.1.2为我修复了它。

可能是因为 typescript 中的 compilerOptions 设置为{“module”: “esnext”} ,所以它被 ts-node 解释为模块。 尝试将其更改为{“module”: “CommonJS”}

尝试使用ts-node-esm index.ts开始您的项目

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM