openai-node icon indicating copy to clipboard operation
openai-node copied to clipboard

Cannot find name `File`.

Open rjamesnw opened this issue 2 years ago • 2 comments

Describe the bug

Imported the openai module and I'm getting an error for an undefined type File.

image

image

To Reproduce

  1. Run npm install openai.
  2. Create a new NodeJS server-side project. image

Code snippets

No response

OS

Windows 10

Node version

v16.6.1

Library version

v3.1.0

rjamesnw avatar Jan 12 '23 21:01 rjamesnw

Note, a temporary workaround is to add type File=any; to the top of the index.d.ts file in the dist directory.

rjamesnw avatar Jan 15 '23 19:01 rjamesnw

Since it looks like you're using Node, in lieu of the workaround, you may be able to use one of the Node runtime configurations from tsconfig/bases, by adding it to your tsconfig.json with "extends".

I ran into this same issue with undefined File type in my Node 16 project, and resolved it by adding "extends": "@tsconfig/node16/tsconfig.json" to my tsconfig.json (full contents of my current working tsconfig.json below, for reference)

{
  "compilerOptions": {
      "module": "commonjs",
      "lib": ["ES2018"],
      "esModuleInterop": true,
      "allowSyntheticDefaultImports": true,
      "target": "es6",
      "noImplicitAny": true,
      "moduleResolution": "node",
      "sourceMap": true,
      "outDir": "dist",
      "baseUrl": ".",
      "paths": {
          "*": [
              "node_modules/*",
              "src/types/*"
          ]
      }
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "src/public"
  ],
  "extends": "@tsconfig/node16/tsconfig.json"
}

TimJohns avatar Feb 13 '23 23:02 TimJohns

It works for me! Thanks!

almerindo avatar Mar 13 '23 18:03 almerindo

Ran into this aswell

ivanglushko avatar Mar 22 '23 20:03 ivanglushko

Create a global.d.ts file in your project's root or type definition folder, then add the following code:

declare global {
  type File = any;
}
export {};

This should fix the issue.

bryanhelmig avatar Mar 28 '23 15:03 bryanhelmig

This is fixed in our upcoming v4, which includes conveniences for file uploads.

rattrayalex avatar Jul 10 '23 01:07 rattrayalex

Non it's not solved still getting error with v4 and using ts-node with tsconfig/paths in Typescript

tsconfig.json

"ts-node": {
    "require": ["tsconfig-paths/register"]
  }
/test/node_modules/openai/_shims/formdata.js:6
exports.File = File;
               ^
ReferenceError: File is not defined
    at Object.<anonymous> (/test/node_modules/openai/_shims/formdata.js:6:16)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Object.require.extensions.<computed> [as .js] (/usr/local/lib/node_modules/ts-node/src/index.ts:1608:43)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Function.Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/test/node_modules/openai/src/uploads.ts:66:15)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)

What i found tsconfig-paths is picking this file _shims/formdata.js but it should pick _shims/formdata.node.js

khanakia avatar Aug 18 '23 07:08 khanakia