Skip to content

@react-pdf/font started to depend on pdfkit@0.20.1 and triggers a TypeError: Invalid URL #3532

Open

Description

@Lucas-C

Describe the bug
Upgrading @react-pdf/font from 4.0.9 to 4.1.0 or 4.1.1 caused a TypeError: Invalid URL

To Reproduce
It is difficult for me to share a reproducible code snippet / example.

In our project, @react-pdf/font is a dependency of @react-pdf/renderer@4.6.0(react@19.2.8) and @react-pdf/types@2.13.1.
We use TypeScript, pnpm, and our code is transpiled using ESBuild.
When invoking Node on the transpiled JS bundle, we get this stracktrace:

TypeError: Invalid URL
    at new URL (node:internal/url:840:25)
    at Object.<anonymous> (/app/node_modules/.pnpm/pdfkit@0.20.1/node_modules/pdfkit/lib/mixins/pdfa.js:3:33)
    at Module._compile (node:internal/modules/cjs/loader:1871:14)
    at Object..js (node:internal/modules/cjs/loader:2002:10)
    at Module.load (node:internal/modules/cjs/loader:1594:32)
    at Module._load (node:internal/modules/cjs/loader:1396:12)
    at wrapModuleLoad (node:internal/modules/cjs/loader:255:19)
    at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5)
    at node:internal/main/run_main_module:33:47

It seems that @react-pdf/font@4.1.0 started to depend on pdfkit@0.20.1, and TypeError: Invalid URL is raised from there:
https://github.com/foliojs/pdfkit/blob/v0.20.1/lib/mixins/pdfa.js#L3

export const ICC_PROFILE_PATH = new URL(
  './data/sRGB_IEC61966_2_1.icc',
  import.meta.url,
).href;

Expected behavior
No exception raised

Desktop (please complete the following information):

  • OS: Linux
  • React-pdf version: @react-pdf/renderer@4.6.0(react@19.2.8)

Activity

  1. changed the title [-]@react-pdf/font started to depend on pdfkit@0.20.1 and triggers a TypeError: Invalid URL for our use case[/-] [+]@react-pdf/font started to depend on pdfkit@0.20.1 and triggers a TypeError: Invalid URL[/+] on Aug 26, 2026
  2. alainrk commented on Sep 10, 2026

    @alainrk

    We hit the same error after upgrading @react-pdf/renderer from 4.3.2 to 4.5.1.
    The failure is still there with the current pdfkit master (0.20.2), so newer renderer versions are affected too.

    Here is a minimal reproduction and some details that may help.

    Reproduction

    Two lines of code, bundled with esbuild for Node (CommonJS output, which is the default when format is not set):

    // entry.js
    import { renderToBuffer } from '@react-pdf/renderer';
    console.log('module loaded', typeof renderToBuffer);
    esbuild entry.js --bundle --platform=node --outfile=out.js
    node out.js

    Result: the bundle throws at module load, before any document is rendered.

    TypeError: Invalid URL
        at new URL (node:internal/url:828:25)
        at Object.<anonymous> (out.js:...)
    

    Versions: Node 24.13, esbuild 0.28.0 (also reproduced with 0.27.2), @react-pdf/renderer 4.5.1, @react-pdf/font 4.1.2, pdfkit 0.20.1.

    What happens

    @react-pdf/font imports pdfkit. Since pdfkit 0.20, the package resolves the import condition for Node to js/pdfkit.node.mjs. That file uses import.meta.url at the top level:

    // pdfkit/lib/mixins/pdfa.js
    export const ICC_PROFILE_PATH = new URL('./data/sRGB_IEC61966_2_1.icc', import.meta.url).href;

    When esbuild produces a CommonJS bundle, import.meta is replaced with an empty object, so the base URL is undefined and new URL() throws.

    There is a second top-level use in the same file, so fixing the ICC line alone is not enough. If the URL line is patched, the bundle then fails on:

    // pdfkit/lib/document.node.js
    const require$1 = createRequire(import.meta.url);
    TypeError [ERR_INVALID_ARG_VALUE]: The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received undefined
    

    This is why the error shows up on the first request in AWS Lambda and similar setups, where the code is usually bundled to CommonJS with esbuild or webpack. @react-pdf/renderer 4.3.x did not have this problem because @react-pdf/font used the @react-pdf/pdfkit fork, which did not rely on import.meta.

    Possible directions

    • In pdfkit: compute ICC_PROFILE_PATH lazily inside _addColorOutputIntent (it is only needed for PDF/A output), and fall back to the ambient require when import.meta.url is undefined, instead of calling createRequire at module load. Both changes are small and keep the ESM build working as it is today.
    • In @react-pdf/font: import the CommonJS entry of pdfkit (pdfkit/js/pdfkit.js) or document that consumers must bundle as ESM.

    Workarounds we found for now: pin @react-pdf/renderer to 4.3.2, or bundle with --format=esm.

    Happy to open a PR on the pdfkit side if that approach is acceptable.

  3. wangzuo commented on Oct 16, 2026

    @wangzuo

    better to pin @react-pdf/font with 4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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