網站搜尋

node.js - 未定義 File:使用 TypeScript 創建 File 物件時


我在 Node.js 中用 TypeScript 編寫了一個程式(不是 API 或網頁應用程式),只是個控制台應用程式。我已經配置了 TypeScript 設定。但當我建立一個 File 物件時,它給我錯誤。

UnhandledPromiseRejectionWarning: ReferenceError: File is not defined

My Code
// .......
var file = new File(["asdasdsa"], 'metadata.json');
console.log(file.name);
// ......

ts 配置 json 內容

    {
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "declaration": true,
    "strict": true,
    "noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
    "strictNullChecks": true /* Enable strict null checks. */,
    "strictFunctionTypes": true /* Enable strict checking of function types. */,
    "noUnusedLocals": true /* Report errors on unused locals. */,
    "noUnusedParameters": true /* Report errors on unused parameters. */,
    "noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
    "noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
    "importHelpers": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "sourceMap": true,
    "outDir": "./dist/tsc/",
    "types": [
      "node",
      "jest"
    ],
    "lib": [
      "ES6",
      "DOM"
    ]
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "**/*.test.ts"
  ]
}

關於 TypeScript 配置,我有遺漏什麼嗎。我想用 TypeScript 建立一個 File 物件

答案#1

我在使用 node v18.15.0 時遇到這個錯誤。更新到 v20.10.0 修正了這個問題,而且在我的 ts-node 腳本中 File 已經被定義,無需任何匯入。