Error occured when using Tesseract.js in NextJS API routes
Unanswered
Asiatic Lion posted this in #help-forum
Asiatic LionOP
Currently I am working on a NextJS app router project and need to integrate OCR using tesseract.js library.
In frontend, user upload an image or pdf file and then in backend, i mean in API routes, receives file from the request and convert it into buffer object and pass it into tesseract.js recognize method.
Code is as shown below:
import { NextResponse } from 'next/server';
import { creatWorker }from "tesseract.js";
//api handler
export async function POST(req: Request) {
const formData = await req.formData();
const file = formData.getAll('file') as File[];
const fileArrayBuffer = await file[0].arrayBuffer();
const buffer = Buffer.from(fileArrayBuffer);
const worker = await createWorker('eng')
const { data: { text }} = await worker.recognize(buffer);
console.log(text)
await worker.terminate();
//do some calculation here with text and get final result
return NextResponse.json(result);
}
And got this error:
Error: Cannot find module 'E:\my-project.next\worker-script\node\index.js'
at Module._resolveFilename (node:internal/modules/cjs/loader:1048:15)
at Module._load (node:internal/modules/cjs/loader:901:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
at MessagePort.<anonymous> (node:internal/main/worker_thread:185:26)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:807:20)
at exports.emitMessage (node:internal/per_context/messageport:23:28) {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
I have tried with various node version (21, 20, 18) and NextJS version (13, 14), but it didn't disappear.
I think this only happens in Nextjs App and I have no idea how to fix it.
Any help is welcome!!!
Thanks in advance.
In frontend, user upload an image or pdf file and then in backend, i mean in API routes, receives file from the request and convert it into buffer object and pass it into tesseract.js recognize method.
Code is as shown below:
import { NextResponse } from 'next/server';
import { creatWorker }from "tesseract.js";
//api handler
export async function POST(req: Request) {
const formData = await req.formData();
const file = formData.getAll('file') as File[];
const fileArrayBuffer = await file[0].arrayBuffer();
const buffer = Buffer.from(fileArrayBuffer);
const worker = await createWorker('eng')
const { data: { text }} = await worker.recognize(buffer);
console.log(text)
await worker.terminate();
//do some calculation here with text and get final result
return NextResponse.json(result);
}
And got this error:
Error: Cannot find module 'E:\my-project.next\worker-script\node\index.js'
at Module._resolveFilename (node:internal/modules/cjs/loader:1048:15)
at Module._load (node:internal/modules/cjs/loader:901:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
at MessagePort.<anonymous> (node:internal/main/worker_thread:185:26)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:807:20)
at exports.emitMessage (node:internal/per_context/messageport:23:28) {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
I have tried with various node version (21, 20, 18) and NextJS version (13, 14), but it didn't disappear.
I think this only happens in Nextjs App and I have no idea how to fix it.
Any help is welcome!!!
Thanks in advance.