Reputation: 21052
MDN lists File
type. Is this type available in Nodejs environment? If yes, directly or should I import some module -- which?
Upvotes: 2
Views: 3760
Reputation: 53
File
is only available in node version 20.x.x or later, here is a comment from a Zod GitHub issue:
Reason and Solution
Using z.instanceof(File)
or anything similar throws File is not defined error, because the File class is only available on node-v20.0.0 or upper (related PR). so if your node is lower than this, and you're trying to run the code on the node environment instead of the browser, it'll throw the File is not defined error. The same goes with FileList
.
So the solution would be to upgrade your node version to the latest one. and if you cannot afford that, u can try using the @web-std/file
library and use the File class they provide
Edit
I tried using file instanceof
File where File is coming from @web-std/file
, and seems it returns false even if file is actually a file. So most probably z.instanceof(File)
will also behave the same. might be an issue from @web-std/file
. So the only solution left for now is to upgrade your node to v-20.0.0
or upper
Source: https://github.com/colinhacks/zod/issues/387#issuecomment-1774603011
Upvotes: 0
Reputation: 32511
File
is part of the Web API, not a part of the standard Javascript language.
As the page you linked to states, normally you'll get instances of File
from a <input type="file" />
. Since Node.js isn't run in a browser, it does not implement any of the browser APIs and as such, does not implement File
. If you want to handle files in Node.js, check out the built-in File System module (fs
).
Upvotes: 2
Reputation: 469
No, due to NodeJS not running in a browser and this being a type defined by the browser it is not available in NodeJS. Use fs instead for files.
Upvotes: 1