I am trying to extract text from the image with tesseract.js node module, i am working with nextjs 14. even though i have install the tesseract.js module i get error that module not find when i try to extract i get this error:
Error: Cannot find module '...(projectdirectory)/.next/worker-script/node/index.js'
at Module._resolveFilename (node:internal/modules/cjs/loader:1142:15)
at Module._load (node:internal/modules/cjs/loader:983:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12)
at MessagePort.<anonymous> (node:internal/main/worker_thread:186:26)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:822:20)
at MessagePort.<anonymous> (node:internal/per_context/messageport:23:28) {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
it should look into the node modules not in .next
so for that i try to change the path of worker-sript in my file
const worker = await createWorker('eng',1,{workerPath: "./node_modules/tesseract.js/src/worker-script/node/index.js"});
but after changing the path there another error get thrown that is:
TypeError: fetch is not a function
i dont know why its happening… from my side everthing is fine, everthing works in my project execpt this.. function
i try to change the path of worker module here is the code :
import {createWorker} from 'tesseract.js';
export const imgToText = async () =>{
//const converter = await createWorker('eng')
const worker = await createWorker('eng',1,{workerPath: "./node_modules/tesseract.js/src/worker-script/node/index.js"});
try {
const img = "/fake.png"
const text = await worker.recognize(img);
console.log(text.data.text);
return text.data.text;
} catch (error) {
console.log("image extraction error", error);
throw error;
} finally {
await worker.terminate();
}
}
i expect text from the image here.
but i got the fetch is not a function so i try to install the node-fetch module but it also didnt worked.
thank you!