zerox icon indicating copy to clipboard operation
zerox copied to clipboard

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

Open KitaharaMugiro opened this issue 3 months ago • 1 comments

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

  • Cleared .next directory and rebuilt the project.
  • Verified dependencies are installed correctly.
  • Ran the application in both development and production modes.

KitaharaMugiro avatar Nov 16 '24 05:11 KitaharaMugiro