文件未定义:使用打字稿创建文件对象时

dus*_*88c 7 file node.js typescript tsconfig

我在 Node js(不是 API 或 Web 应用程序)中用 typescript 编写了一个程序,只是一个控制台应用程序。我已经配置了打字稿配置。但是当我创建文件对象时它给了我错误

UnhandledPromiseRejectionWarning: ReferenceError: File is not defined

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

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"
  ]
}
Run Code Online (Sandbox Code Playgroud)

我在打字稿配置上错过的任何东西。我想从打字稿创建一个文件对象

Sea*_*ean 0

我在使用 node 时遇到了此错误v18.15.0。更新v20.10.0为我修复了这个问题,并File在我的 ts-node 脚本中定义,无需导入。