#Error: "Node-Fetch needs to imported"

6 messages · Page 1 of 1 (latest)

cerulean flareBOT
rancid zinc
#

Hello!

I'm getting an error while building my Next.js project: 'Module not found: ESM packages (node-fetch) need to be imported.' It seems that node-fetch is imported incorrectly in the file getExternalFile.js. The error spreads across files in the payload and @graceful vale/next-payload modules, ending up in me.ts.

How can I resolve this error and properly import node-fetch as an ES module?

Here is my code for "./node_modules/payload/dist/uploads/getExternalFile.js:61:108":

const getExternalFile = async ({ data, req })=>{
const { filename, url } = data;
if (typeof url === 'string') {
let fileURL = url;
if (!url.startsWith('http')) {
const baseUrl = req.get('origin') || ${req.protocol}://${req.get('host')};
fileURL = ${baseUrl}${url};
}
const { default: fetch } = await Promise.resolve().then(()=>/#PURE/ _interop_require_wildcard(require("node-fetch")));
const res = await fetch(fileURL, {
credentials: 'include',
headers: {
...req.headers
},
method: 'GET'
});
if (!res.ok) throw new _errors.APIError(Failed to fetch file from ${fileURL}, res.status);
const data = await res.buffer();
return {
name: filename,
data,
mimetype: res.headers.get('content-type') || undefined,
size: Number(res.headers.get('content-length')) || 0
};
}
throw new _errors.APIError('Invalid file url', 400);
};

foggy sparrow
#

How is it importe din the pre-compiled code?

rancid zinc
#

@foggy sparrow

tidal bobcat
#

I have run into it and i dont know why