我在 Node js 中用 TypeScript 编写了一个程序(不是 API 或 Web 应用程序),只是一个控制台应用程序。我已经配置了打字稿配置。但是当我创建文件对象时它给了我错误
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"
]
}
我在打字稿配置上错过的任何东西。 我想从打字稿创建一个文件对象
我在节点
v18.15.0
上遇到了此错误。更新到 v20.10.0
为我解决了这个问题,并且 File
是在我的 ts-node 脚本中定义的,无需导入。