【发布时间】:2021-09-04 05:10:24
【问题描述】:
我在 node js(不是 API 或 Web 应用程序)中编写了一个带有 typescript 的程序,只是一个控制台应用程序。我已经配置了打字稿配置。但是当我创建一个文件对象时它给了我错误
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 配置中遗漏的任何内容。 我想用打字稿制作一个 File 对象
【问题讨论】:
-
我也有同样的问题。运行 ts-node
-
node.js 没有实现 File 类(或 FileList 等)。有一些模块可以模拟它,但在服务器端,无论您使用 TS 还是 JS,这都不是仅使用节点的选项。
-
我可以使用哪个模块来完成这项工作。 @乔谢谢
标签: node.js typescript file tsconfig