Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
KitaharaMugiro opened this issue Nov 16, 2024 · 4 comments

Comments

@KitaharaMugiro
Copy link

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.
@tylermaran
Copy link
Contributor

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/

@abdulrehmandev
Copy link

Hi guys! this issue is happening to me too. I was also working with Nextjs Server actions, now I have switched to /api to test things out. Previously there were issues related with Sharp package which I was able to resolve by using older Node version. but this is something that has got my head spinning.

Current API:

import { NextRequest, NextResponse } from "next/server";
import path from "path";
import fs from "fs/promises";

export async function POST(request: NextRequest) {
  try {
    const formData = await request.formData();
    const file = formData.get("file") as File | null;

    if (!file) {
      return NextResponse.json({ error: "File is required!" }, { status: 400 });
    }

    // Create a temporary file path
    const tempDir = process.cwd() + "/temp";
    await fs.mkdir(tempDir, { recursive: true });
    const tempFilePath = path.join(tempDir, `${Date.now()}-${file.name}`);

    // Convert file to buffer and save
    const bytes = await file.arrayBuffer();
    const buffer = Buffer.from(bytes);
    await fs.writeFile(tempFilePath, buffer);

    try {
      // Dynamically import zerox
      const { zerox } = await import("zerox");

      const { pages } = await zerox({
        filePath: tempFilePath, // Use the uploaded file path
        openaiAPIKey: process.env.OPENAI_KEY,
      });

      // Clean up the temporary file
      await fs.unlink(tempFilePath);

      return NextResponse.json({
        pages,
        pageCount: pages.length,
      });
    } catch (processingError) {
      // cleanup
      await fs.unlink(tempFilePath);

      return NextResponse.json(
        {
          error: "Failed to process file",
          details:
            processingError instanceof Error
              ? processingError.message
              : String(processingError),
        },
        { status: 500 }
      );
    }
  } catch (error) {
    return NextResponse.json(
      {
        error: "Failed to upload file",
        details: error instanceof Error ? error.message : String(error),
      },
      { status: 500 }
    );
  }
}

export const config = {
  api: {
    bodyParser: false, // Disable default body parser
  },
  maxDuration: 300, // 5 minutes max execution time
};

Versions

  • "next": "15.0.3"
  • "zerox": "^1.0.38"

Additionally
The .next/worker-script folder doesn't exist in the .next folder

If anyone can help, or has already worked with this, help out!!

@alexander-densley
Copy link
Contributor

@tylermaran do you guys use zerox in nextjs? If so would it be possible to have an example repo of using it?

@antonyadhiban
Copy link

Not sure if this is the exact issue @tylermaran @alexander-densley .
But adding the serverExternalPacakges in the next.config.js got me past the missing modules error. But still stuck with axios errors.

`/** @type {import('next').NextConfig} */
const nextConfig = {
serverExternalPackages: ['zerox'],
}

module.exports = nextConfig `

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants