Skip to content

Windows/Next.js: ESM loader error with worker path protocol #79

@Mavni

Description

@Mavni

Environment

  • Node.js version: 24.x
  • pdf-parse version: 2.4.5
  • OS: Windows 11
  • Framework: Next.js 16.0.10 (Turbopack)

Description

When using pdf-parse v2.4.5 in a Next.js API route on Windows, the worker setup fails with an ESM loader protocol error.

Error Message

Setting up fake worker failed: "Only URLs with a scheme in: file, data, and node are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'e:'"

Steps to Reproduce

  1. Create a Next.js project with an API route that uses pdf-parse v2
  2. Use the documented approach from troubleshooting:
const { CanvasFactory, getPath } = require('pdf-parse/worker');
const { PDFParse } = require('pdf-parse');

PDFParse.setWorker(getPath());  // Fails on Windows
// Also tried:
PDFParse.setWorker(getData());  // Same error
  1. Call the API route to parse a PDF

Expected Behavior

The worker should initialize correctly using either getPath() or getData().

Actual Behavior

The worker fails to set up because the path returned by getPath() uses a Windows drive letter (e.g., e:\path\to\worker.js) instead of a proper file:// URL that the ESM loader expects.

Possible Fix

The getPath() function should convert Windows paths to proper file:// URLs using Node.js url.pathToFileURL():

import { pathToFileURL } from 'node:url';

function getPath() {
  const workerPath = /* current implementation */;
  return pathToFileURL(workerPath).href;
}

Workaround

Switched to unpdf which includes a serverless-optimized pdfjs-dist build with inlined worker that works correctly on Windows and Next.js.

Additional Context

  • Added serverExternalPackages: ['pdf-parse', '@napi-rs/canvas'] to next.config.js as documented
  • Tried both ESM and CJS import patterns
  • Node.js ESM loader on Windows requires file:// protocol for absolute paths

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions