getomni-ai / zerox

PDF to Markdown with vision models
https://getomni.ai/ocr-demo
MIT License
6.22k stars 340 forks source link

Error: Cannot find module '/.next/worker-script/node/index.js' when using server functions with zerox in Next.js #99

Open KitaharaMugiro opened 5 hours ago

KitaharaMugiro commented 5 hours ago

Bug Report

Description

An error occurs when running a server function in a Next.js application using zerox. The issue is related to a missing module: /Users/mugiro/ai-chatbot/.next/worker-script/node/index.js.

Reproduction

  1. Use the following server-side code:

    'use server';
    
    import fs from 'fs';
    import path from 'path';
    import { zerox } from 'zerox';
    
    export const processFile = async (formData: FormData) => {
       console.log('processFile started');
    
       // Save file
       const file = formData.get('file');
       const tempDir = path.join(process.cwd(), 'uploads');
       if (!fs.existsSync(tempDir)) fs.mkdirSync(tempDir);
    
       if (!file || !(file instanceof File)) {
           throw new Error('No file uploaded');
       }
    
       const filePath = path.join(tempDir, file.name);
       const buffer = Buffer.from(await file.arrayBuffer());
       fs.writeFileSync(filePath, buffer);
    
       // Extract text using zerox
       const result = await zerox({
           filePath: "https://omni-demo-data.s3.amazonaws.com/test/cs101.pdf",
           openaiAPIKey: process.env.OPENAI_API_KEY,
       });
    
       const dataDir = path.join(process.cwd(), 'data');
       if (!fs.existsSync(dataDir)) fs.mkdirSync(dataDir);
    
       const text = result.pages.map(page => page.content).join('\n\n');
       fs.writeFileSync(path.join(dataDir, 'text.txt'), text);
    };
  2. Run the application.

  3. Observe the following error logs in the terminal:

    [Error: Cannot find module '/Users/mugiro/ai-chatbot/.next/worker-script/node/index.js'] {
     code: 'MODULE_NOT_FOUND',
     requireStack: []
    }

Expected Behavior

The processFile function should process the uploaded file, extract text using zerox, and save the extracted text to a file without errors.

Actual Behavior

The application crashes with the error:

[Error: Cannot find module '/Users/mugiro/ai-chatbot/.next/worker-script/node/index.js'] {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Additional Context

The issue might be related to the .next/worker-script directory not being correctly generated or referenced. The error occurs consistently, even after clearing .next and rebuilding the project.

Steps Taken

tylermaran commented 5 hours ago

Hey @KitaharaMugiro. This problem might be outside of zerox. Zerox shouldn't rely on any worker-script functionality.

Can you try logging what is in your next/worker-script directory?

ls -R .next/worker-script/node/