openai-node
openai-node copied to clipboard
Cannot find name `File`.
Describe the bug
Imported the openai
module and I'm getting an error for an undefined type File
.
To Reproduce
- Run
npm install openai
. - Create a new NodeJS server-side project.
Code snippets
No response
OS
Windows 10
Node version
v16.6.1
Library version
v3.1.0
Note, a temporary workaround is to add type File=any;
to the top of the index.d.ts
file in the dist
directory.
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"
}
It works for me! Thanks!
Ran into this aswell
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.
This is fixed in our upcoming v4, which includes conveniences for file uploads.
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