Skip to content

Dev: client optimizeDeps excludes serverExternalPackages, which breaks optional requires in client deps (linkedom → canvas) #3484

Open

Description

@kstulgys

vinext 1.0.0-beta.12 (also on main at c4b705e), App Router, Vite 8.3.1.

The client environment's optimizeDeps.exclude includes the whole default serverExternalPackages list (packages/vinext/src/index.ts, the client environment in the App Router config). canvas is on that list. A client dependency that requires canvas optionally, inside a try/catch, then breaks in vinext dev:

linkedom's commonjs/canvas.cjs:

try { module.exports = require('canvas'); } catch (fallback) { module.exports = require('./canvas-shim.cjs'); }

With canvas excluded, the dep optimizer treats it as external and hoists the require into a top-level import * as m from "canvas" in .vite/deps/linkedom.js. Vite resolves that import to __vite-optional-peer-dep:canvas:linkedom, which throws on evaluation, outside the try. Any client module that imports linkedom fails to load:

Error: Could not resolve "canvas" imported by "linkedom".
    at http://localhost:5174/@id/__vite-optional-peer-dep:canvas:linkedom:2:7

Plain Vite (no exclusion) bundles the optional peer dep inside the try, and the shim is used. vinext build is not affected.

Repro: an App Router client component that does import("linkedom") in the browser, canvas not installed, vinext dev.

Workaround: a plugin whose configEnvironment hook removes canvas from the client environment's optimizeDeps.exclude.

Suggested fix: exclude server-external packages from the client optimizer only when they are imported directly from app code, or do not apply the list to packages that are reached only through a dependency's require.

Activity

  1. james-elicx commented on Sep 29, 2026

    @james-elicx
    Member

    I submitted vitejs/vite#23600 to Vite which should fix this, so just pending new Vite releases.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions