Open ntpnhan opened 8 months ago
Hi !!
I have the same problem, your have been able to solve it?
Cause: Response is used in the package, but is not imported. I temporarily solved the problem in this way and hope the author can fix it later.
/node_modules/@imgly/background-remove-node/dist/index.mjs
file .
Add in header import {Response, Blob} from "node-fetch"
; npm i node-fetch
There is a more reasonable solution :) Copy this code into your entry file, such as index.mjs. It works on my computer.
import { removeBackground } from "@imgly/background-removal-node";
import fs from "fs";
// here
import { Response, Blob } from "node-fetch";
globalThis.Response = Response;
globalThis.Blob = Blob;
const image_src = "./1_9it-TQH191NXVvKqv4dF1A.webp";
async function removeImageBackground(imgSource) {
const blob = await removeBackground(imgSource, {
model: "medium",
progress: (progress, current, total) => {
const currentProgress = (current / total) * 100;
console.log(progress, `Progress: ${currentProgress.toFixed(2)}%`);
},
});
const buffer = Buffer.from(await blob.arrayBuffer());
const dataURL = `data:image/png;base64,${buffer.toString("base64")}`;
return dataURL;
}
removeImageBackground(image_src)
.then((dataURL) => {
fs.writeFileSync("output.png", dataURL.split(";base64,").pop(), {
encoding: "base64",
});
})
.catch((error) => {
console.error(error);
});
I'm using @imgly/background-removal-node package for an example. But I get the error: ReferenceError: Response is not defined Following is my example
How to resolve this? Please