Skip to content

nf3 drops @img/sharp-libvips-* during trace → sharp fails with ERR_DLOPEN_FAILED #47

Closed

Description

@ulrichstark

Environment

System:
OS: macOS 26.3
CPU: (10) arm64 Apple M4
Memory: 114.84 MB / 16.00 GB
Shell: 5.9 - /bin/zsh

Binaries:
Node: 26.1.0 - /Users/ulrichstark/.nvm/versions/node/v26.1.0/bin/node
npm: 11.13.0 - /Users/ulrichstark/.nvm/versions/node/v26.1.0/bin/npm
pnpm: 11.5.2 - /Users/ulrichstark/.nvm/versions/node/v26.1.0/bin/pnpm
bun: 1.2.16 - /Users/ulrichstark/.bun/bin/bun
Deno: 2.7.12 - /Users/ulrichstark/.deno/bin/deno

Reproduction

https://github.com/ulrichstark/nf3-sharp-libvips-repro

Describe the bug

The following is AI generated and reviewed by me. I hope this helps to understand the issue.

traceNodeModules silently omits the platform-specific libvips package that sharp's native addon links
against at dlopen time, so the traced output cannot load sharp at runtime.

This surfaces to end users through Nitro / TanStack Start production builds (vite build
→ .output/server), where any route using sharp throws:

Could not load the "sharp" module using the darwin-arm64 runtime
ERR_DLOPEN_FAILED: ... Library not loaded: @rpath/libvips-cpp.8.18.3.dylib

Versions

  • [email protected] (latest published; main is also affected — see below)
  • [email protected] → @img/sharp-libvips-*@1.3.0
  • Reproduced on darwin-arm64; the cause is not platform-specific (all
    @img/sharp-libvips-*@1.3.0 packages ship the same exports map).

Run

git clone https://github.com/ulrichstark/nf3-sharp-libvips-repro
npm install
npm run repro   # exits 1 and prints "BUG REPRODUCED"

Expected

@img/sharp-libvips-<platform> is copied into the trace output (its
lib/libvips-cpp.*.dylib / .so is what the addon loads), so sharp works.

Actual

Installed @img packages: colour, sharp-darwin-arm64, sharp-libvips-darwin-arm64
Traced    @img packages: colour, sharp-darwin-arm64

Resolving @img/sharp-libvips-darwin-arm64 from @img/sharp-darwin-arm64:
  /package.json -> undefined   <-- nf3 uses this
  /package      -> .../@img/sharp-libvips-darwin-arm64/package.json

[FAIL] BUG REPRODUCED: @img/sharp-libvips-* was not copied into the trace output.

Root cause

sharp's prebuilt addon @img/sharp-darwin-arm64/lib/*.node links
libvips-cpp.*.dylib via @rpath. That dylib lives in a separate package,
@img/sharp-libvips-darwin-arm64, reached only through the native binary — there
is no JS import/require edge to it, so @vercel/nft never traces it.

nf3 has a fallback for exactly this case: after tracing, it walks each package's
optionalDependencies and full-copies any that weren't traced
(src/trace.ts, the
"Auto-detect and full-trace optionalDependencies" block). @img/sharp-darwin-arm64
declares @img/sharp-libvips-darwin-arm64 as an optionalDependencies, so this
fallback should pick it up.

It fails on how the package is located:

const resolved = resolveModulePath(depName + "/package.json", {
  try: true,
  from: versionEntry.path,
});
if (!resolved) continue; // <-- libvips bails here and is never copied

The @img/sharp-libvips-* packages publish a restrictive exports map:

"exports": {
  "./lib": "./lib/index.js",
  "./package": "./package.json",
  "./versions": "./versions.json"
}

There is no "./package.json" entry (and no "."), so Node/exsolve correctly
refuses to resolve @img/sharp-libvips-darwin-arm64/package.json. The resolve
returns undefined, the continue fires, and the dylib package is silently
dropped from the output. The repro prints both resolutions side by side: /package.json
is undefined while /package (which also points at package.json) resolves fine.

Why this regressed with [email protected]

The restrictive exports is not new (libvips has shipped it since 1.0.4). What
changed is that older sharp reached the libvips package through its JS graph, so
@vercel/nft traced it directly and the broken optionalDependencies fallback never
mattered. With [email protected] the only reachable libvips reference is the native
@rpath link, so the optionalDependencies walk becomes the sole path — and it's broken.

Suggested fix

Locate the optional dependency without assuming <pkg>/package.json is an exposed
subpath. Any of:

  • resolve depName + "/package" as a fallback when depName + "/package.json" fails
    (the libvips packages, and many native packages, expose ./package), or
  • resolve the package root / directory rather than the package.json subpath
    (e.g. via the package's directory in node_modules), then read
    join(dir, "package.json") directly off disk — the same pattern already used a
    few lines earlier when building pkgJSON for traced packages.

Workaround (downstream)

Until nf3 is fixed, copy the installed @img/sharp-libvips-* package(s) into the
server output after the build. With Nitro, a compiled hook works:

import { cp, readdir } from "node:fs/promises";
import { join } from "node:path";

nitro({
  hooks: {
    async compiled(nitro) {
      const imgDir = join(nitro.options.rootDir, "node_modules", "@img");
      const entries = await readdir(imgDir).catch(() => []);
      await Promise.all(
        entries
          .filter((name) => name.startsWith("sharp-libvips-"))
          .map((name) =>
            cp(
              join(imgDir, name),
              join(nitro.options.output.serverDir, "node_modules", "@img", name),
              { recursive: true },
            ),
          ),
      );
    },
  },
});

Additional context

No response

Logs

Running `node repro.mjs` logs:

Trace inputs:
  - /Users/ulrichstark/Desktop/nf3-sharp-libvips-repro/node_modules/sharp/dist/index.mjs
  - /Users/ulrichstark/Desktop/nf3-sharp-libvips-repro/node_modules/@img/sharp-darwin-arm64/index.cjs

Installed @img packages: colour, sharp-darwin-arm64, sharp-libvips-darwin-arm64
Traced    @img packages: colour, sharp-darwin-arm64

Resolving @img/sharp-libvips-darwin-arm64 from @img/sharp-darwin-arm64:
  /package.json -> undefined   <-- nf3 uses this
  /package      -> /Users/ulrichstark/Desktop/nf3-sharp-libvips-repro/node_modules/@img/sharp-libvips-darwin-arm64/package.json

[FAIL] BUG REPRODUCED: @img/sharp-libvips-* was not copied into the trace output.
       sharp will fail at runtime with ERR_DLOPEN_FAILED (Library not loaded:
       @rpath/libvips-cpp.*.dylib).

Activity

  1. added
    bug
    on Jun 17, 2026
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions