Skip to content

Remote Functions: Prerender error on build #14708

@Adaphath

Description

@Adaphath

Describe the bug

I am building a SvelteKit library and use remote functions in there to export them for other packages to consume it.

It results in an error when trying to build the library or consuming package WITHOUT an import of the .remote.ts file but other imports from the barrel export (index.ts).

SvelteKit version: 2.43.7
Svelte version: 5.39.8

Error:

node:internal/event_target:1101
  process.nextTick(() => { throw err; });
                           ^
TypeError [Error]: Cannot convert undefined or null to object
    at Function.values (<anonymous>)
    at prerender (file:///PATH/node_modules/.pnpm/@sveltejs+kit@2.43.7_@sveltejs+vite-plugin-svelte@6.2.0_svelte@5.39.8_vite@7.1.5_@types_84e55b3de3627de376d558012584fce6/node_modules/@sveltejs/kit/src/core/postbuild/prerender.js:502:27)
    at async MessagePort.<anonymous> (file:///PATH/node_modules/.pnpm/@sveltejs+kit@2.43.7_@sveltejs+vite-plugin-svelte@6.2.0_svelte@5.39.8_vite@7.1.5_@types_84e55b3de3627de376d558012584fce6/node_modules/@sveltejs/kit/src/utils/fork.js:23:16)

From some manual hacking in the prerender.js file at line 500:

for (const loader of Object.values(manifest._.remotes)) {
	const module = await loader();

  console.log(module);

  if (!module.default) {
    console.error('Module not found');
    continue;
  }

	for (const fn of Object.values(module.default)) {
		if (fn?.__?.type === 'prerender') {
			prerender_functions.push(fn.__);
			should_prerender = true;
		}
	}
}

Logs from building with a manual check:

...
.svelte-kit/output/client/_app/immutable/entry/app.DWw0zJhL.js        26.13 kB │ gzip:  8.70 kB
.svelte-kit/output/client/_app/immutable/chunks/BpSA-ywl.js           33.99 kB │ gzip: 13.34 kB
✓ built in 769ms
[Module: null prototype] {  }
Module not found
[Module: null prototype] {
  a: [Function: wrapper],
  default: [Object: null prototype] [Module] {
    countCollection: [Getter],
    getCollection: [Getter],
    getDocument: [Getter]
  },
  g: [Function: wrapper]
}
.svelte-kit/output/server/.vite/manifest.json                                                                    9.78 kB
...

I thought this relates to #14444 and tried to describe my issue before.
But I now got a reproduction ready.

Key insight:
Due to the side-effects in package.json the file gets tree-shaked by the bundler but SvelteKit expects it to load in the prerender.js where it gets an empty module and errors out

Reproduction

StackBlitz reproduction repo:
https://stackblitz.com/edit/sveltejs-kit-template-default-uqvtwu55?file=src%2Flib%2Findex.ts

Try using npm run build this will fail with an error.
It can be "fixed" by:

  • removing the side-effects in package.json
  • not importing the noop() function in the hooks.server.ts. Then it will build normally.
  • disabling remote functions in svelte.config.js

Logs

System Info

System:
    OS: macOS 26.1
    CPU: (12) arm64 Apple M2 Pro
    Memory: 124.52 MB / 32.00 GB
    Shell: 3.7.1 - /opt/homebrew/bin/fish
  Binaries:
    Node: 22.20.0 - /Users/sebi/.local/share/nvm/v22.20.0/bin/node
    npm: 10.9.3 - /Users/sebi/.local/share/nvm/v22.20.0/bin/npm
    pnpm: 10.7.0 - /opt/homebrew/bin/pnpm
    bun: 1.2.13 - /opt/homebrew/bin/bun
  Browsers:
    Chrome: 141.0.7390.67
    Firefox: 143.0.4
    Safari: 26.1
  npmPackages:
    @sveltejs/adapter-auto: ^6.0.0 => 6.0.1
    @sveltejs/adapter-node: ^5.3 => 5.3.2
    @sveltejs/kit: 2.42.1 => 2.46.5
    @sveltejs/package: ^2.0.0 => 2.5.4
    @sveltejs/vite-plugin-svelte: ^6.0.0 => 6.2.1
    svelte: ^5.38.0 => 5.39.11
    vite: ^7.0.4 => 7.1.9

Severity

serious, but I can work around it

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions