File is not defined : when create File Object with typescript
Asked Answered
N

1

7

I write a program with typescript in node js (not an API or web app) just a console app. I have configured the typescript configuration. but when I make a File Object it give me error

UnhandledPromiseRejectionWarning: ReferenceError: File is not defined

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

ts config json content

    {
  "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"
  ]
}

Anything I missed on typescript configuration. I want to make an object of File from typescript

Nietzsche answered 4/9, 2021 at 5:10 Comment(4)
I have the same problem. running ts-nodeAchromaticity
node.js does not implement the File class (or FileList etc). There are modules that emulate it but server-side this won't be an option just using node whether or not you use TS or JS.Nert
which module I can use to make this done. @joe thanksNietzsche
github.com/abrwn/get-file-object-from-local-path may be this works , will tryNietzsche
A
1

I was getting this error with node v18.15.0. Updating to v20.10.0 fixed this for me, and File was defined in my ts-node script with no imports needed.

Audun answered 22/1 at 19:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.