簡體   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