Skip to content

"Error: Server-only module referenced by client" if imports go unused *and* dir is exactly ".server" (no prefix) #14997

@fatmalama

Description

@fatmalama

Reproduction

I've made an example on StackBlitz: https://stackblitz.com/edit/github-o99gfxz6?file=app%2Froutes%2Fhome.tsx

it's quite an odd bug: if you make a ".server" directory (without prefixing it with a name) and you import something from it but not use it, it will fail with a [plugin react-router:dot-server] Error: Server-only module referenced by client

if you do use everything you import from it everything will be fine. Or the dir is prefixed ("whatever.server") even if you leave imports unused (!!).

note that making an unprefixed "**/.server" dir is explicitly mentioned in the documentation and as the only example given on the server modules page for a server dir actually

System Info

(from the StackBlitz repo)

~/projects/github-o99gfxz6
❯ npx envinfo --system --npmPackages '{vite,react-router,@react-router/*}' --binaries --browsers
Need to install the following packages:
envinfo@7.21.0
Ok to proceed? (y) y


  System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 22.22.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.8.2 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    @react-router/dev: 7.14.0 => 7.14.0 
    @react-router/node: 7.14.0 => 7.14.0 
    @react-router/serve: 7.14.0 => 7.14.0 
    react-router: 7.14.0 => 7.14.0 
    vite: ^8.0.3 => 8.0.9

Used Package Manager

npm

Expected Behavior

not throwing "Server-only module referenced by client" since it is misleading and it doesn't happen on prefixed dirs.server/** but only on dirs/.server/**

at most, a warning about unused imports which should also show up for dirs/.server/**

Actual Behavior

build failure. log from running npm run build on StackBlitz:

Build failed with 1 error:

Error: Server-only module referenced by client

    '../.server/fun' imported by route 'app/routes/home.tsx'

  React Router automatically removes server-code from these exports:
    `loader`, `action`, `middleware`, `headers`

  But other route exports in 'app/routes/home.tsx' depend on '../.server/fun'.

  See https://reactrouter.com/explanation/code-splitting#removal-of-server-code
    at napi_create_error (/tmp/rolldown-1.0.0-rc.16/node_modules/.pnpm/@emnapi+core@1.9.2/node_modules/@emnapi/core/dist/emnapi-core.cjs.min.js:1:77075)
    at null.<anonymous> (wasm://wasm/02e2eb7e:1:6178377)
    at null.<anonymous> (wasm://wasm/02e2eb7e:1:4524065)
    at null.<anonymous> (wasm://wasm/02e2eb7e:1:9919657)
    at null.<anonymous> (wasm://wasm/02e2eb7e:1:5406559)
    at null.<anonymous> (wasm://wasm/02e2eb7e:1:7328612)
    at null.<anonymous> (wasm://wasm/02e2eb7e:1:5940205)
    at null.<anonymous> (wasm://wasm/02e2eb7e:1:6667456)
    at null.<anonymous> (wasm://wasm/02e2eb7e:1:10224426)
    at null.<anonymous> (wasm://wasm/02e2eb7e:1:3445528)
    at aggregateBindingErrorsIntoJsError (file:///home/projects/github-o99gfxz6/node_modules/rolldown/dist/shared/error-DAA7ncC5.mjs:67:18)
    at Module.unwrapBindingResult (file:///home/projects/github-o99gfxz6/node_modules/rolldown/dist/shared/error-DAA7ncC5.mjs:37:128)
    at RolldownBuild.#build (file:///home/projects/github-o99gfxz6/node_modules/rolldown/dist/shared/rolldown-build-BPKCFYpX.mjs:3343:54)
    at buildEnvironment (file:///home/projects/github-o99gfxz6/node_modules/vite/dist/node/chunks/node.js:33072:64)
    at Object.build (file:///home/projects/github-o99gfxz6/node_modules/vite/dist/node/chunks/node.js:33494:19)
    at buildEnvironment (/home/projects/github-o99gfxz6/node_modules/@react-router/dev/dist/cli/index.js:2065:5)
    at viteBuild (/home/projects/github-o99gfxz6/node_modules/@react-router/dev/dist/cli/index.js:2096:3)
    at build (/home/projects/github-o99gfxz6/node_modules/@react-router/dev/dist/cli/index.js:1962:10)
    at build2 (/home/projects/github-o99gfxz6/node_modules/@react-router/dev/dist/cli/index.js:2300:5)
    at run2 (/home/projects/github-o99gfxz6/node_modules/@react-router/dev/dist/cli/index.js:2631:7) {
  errors: [Getter/Setter]
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions