remote function build issues sveltekit @ 2.39.0 and up (tested up to and including 2.42.0)
#14444
Development PRs
It is currently possible for a chunk to import chunks that import itself. I'm not exactly sure where this occurs or why as I can't reproduce it in a simple repository, but a simple fix is to prevent importing already visited chunks.
This addresses #14444
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
- It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- This message body should clearly illustrate what problems it solves.
- Ideally, include a test that fails without this PR but passes with it.
Tests
- Run the tests with
pnpm testand lint the project withpnpm lintandpnpm check
Changesets
- If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.
Edits
- Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.
Instead of using manualChunks which turns out to cause some weird chunking bugs, we instead use a virtual module which we force to be loaded at the very end after all other modules have been discovered. By that time we have collected all remote function files and can generated a record of hash->import. Vite will then do the proper chunking without further interference by us. Thanks @bluwy for the tip!
Fixes #14444 and possibly #14430 and #14519
Putting this into draft because while working on this I discovered that remote functions in node_modules are not loaded at dev time - but this is not new to this PR, it's failing on main, too. Investigating. Also fixes a bug in dev mode where remote functions might not be transformed if Vite decides to append a query parameter (we checked if the file ends with .remote.js/ts but they could end with .remote.js/ts?v=123)
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
- It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- This message body should clearly illustrate what problems it solves.
- Ideally, include a test that fails without this PR but passes with it.
Tests
- Run the tests with
pnpm testand lint the project withpnpm lintandpnpm check
Changesets
- If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.
This is my attempt to provide an alternative to #14456, which is extremely clever but makes me very nervous. It works, but as soon as another plugin tries the same trick (repeatedly getting a list of every module in the graph and loading it, until there are no new modules to load) you end up in a deadlock situation. The 30 second timeout really isn't a solution. It's almost certain that very few users would ever encounter this, but for the ones that did it would be a very confusing and annoying bug.
This PR sticks with the manualChunks trick, but adds another layer: any modules imported by .remote.ts files are also put in their own chunks. This has the effect of forcing each chunk corresponding to a .remote.ts module to only contain that module, and none of the dependencies that Rollup incorrectly deems safe to include in the chunk.
My hunch — or at least, my hope — is that Rollup gets confused about how to structure the graph because it's not expecting these chunks to be treated as entries (resulting in the bad kind of circular dependency) and that this change renders that moot. It's possible that I haven't fixed it at all, but have rather pushed the bug 'one level down' so to speak, but I think it's worth a shot.
Fixes #14444 and possibly #14430
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
- It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- This message body should clearly illustrate what problems it solves.
- Ideally, include a test that fails without this PR but passes with it.
Tests
- Run the tests with
pnpm testand lint the project withpnpm lintandpnpm check
Changesets
- If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.
Edits
- Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.
Issue
Describe the bug
Running vite build works in sveltekit @2.38.1 but fails in 2.39.0 -> 2.41.0 versions the issue appears to occur due to changes in https://github.com/sveltejs/kit/blob/3120d170b157b5a00369b3ee66d3cbe4d3c6fc03/packages/kit/src/exports/vite/index.js
These are the errors I am seeing:
[plugin vite-plugin-sveltekit-remote] Sourcemap is likely to be incorrect: a plugin (vite-plugin-sveltekit-remote) was used to transform files, but didn't generate a sourcemap for the transformation. Consult the plugin documentation for help (x4)
and
node:internal/event_target:1101
process.nextTick(() => { throw err; });
^
ReferenceError [Error]: Cannot access 'info_v2' before initialization
at file:///Users/em/Dev/app/.svelte-kit/output/server/chunks/remote-eooeoj.js:1288:91
at ModuleJob.run (node:internal/modules/esm/module_job:345:25)
at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:651:26)
at async analyse (file:///Users/em/Dev/app/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:51:19)
at async MessagePort.<anonymous> (file:///Users/em/Dev/app/node_modules/@sveltejs/kit/src/utils/fork.js:23:16)
Reproduction
This is difficult to reproduce as it seems to only occur when the generated remote files are large and split into multiple files.
Logs
System Info
System:
OS: macOS 15.6
CPU: (12) arm64 Apple M2 Max
Memory: 988.89 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 22.19.0 - ~/.volta/tools/image/node/22.19.0/bin/node
Yarn: 4.9.4 - ~/.volta/tools/image/yarn/4.9.4/bin/yarn
npm: 11.6.0 - ~/.volta/tools/image/npm/11.6.0/bin/npm
bun: 1.2.0 - ~/.volta/bin/bun
Browsers:
Brave Browser: 121.1.62.156
Edge: 140.0.3485.66
Safari: 18.6
npmPackages:
@sveltejs/adapter-node: ^5.3.2 => 5.3.2
@sveltejs/enhanced-img: ^0.8.1 => 0.8.1
@sveltejs/kit: 2.41.0 => 2.41.0
@sveltejs/vite-plugin-svelte: ^6.2.0 => 6.2.0
svelte: 5.38.10 => 5.38.10
vite: 7.1.5 => 7.1.5
Severity
blocking an upgrade
Additional Information
Not that this helps much but the line in question occurs in what is essentially a compiled version of a bunch of drizzle queries in one big file. Specifically it looks like this.
const { date: _info_date, ...provider_info_v2_columns } = getTableColumns(info_v2);
Well above that line 1288, on line 27 is
import { i as info_v2, ... } from "./remote-12p15ba.js";
And that remote file seems to be the drizzle schema, at the end of the file.
export {
info_v2 as i,
...
}
So the output seems correct but for whatever reason it is complaining that the drizzle table has not yet been initialized, which leads me to believe it is attempting to run the code in the build environment without the imports.
Info
The additional information part might or might not be related to #14439
I think these changes are the cause. https://github.com/sveltejs/kit/compare/%40sveltejs/kit%402.38.1...%40sveltejs/kit%402.39.0#diff-beef34c36ed87d7835d0166655010910fba9949a6ef95042cb468d7d73423816
One more thing I forgot to mention, the issue doesn't occur in vite dev, it is a vite build specific issue.
2.42.0 didn't contain the fix yet, 2.42.1 which I just released does. Could you check again?
I've got good news and bad news. That fixes the cannot access before initialization error. However it has been replaced with the following:
node:internal/event_target:1101
process.nextTick(() => { throw err; });
^
RangeError [Error]: Maximum call stack size exceeded
at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:259:16)
at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:271:5)
at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:271:5)
at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:271:5)
at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:271:5)
at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:271:5)
at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:271:5)
at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:271:5)
at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:271:5)
at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:271:5)
also the sourcemap warning is still present (though admitantly unrelated)
We really need a minimum reproduction (or any kind of reproduction) in that case.
I'm going to try and figure something out tonight to do so, that or I'll stuff logs into my node packages.
Still can't reproduce... but I have some more insight, the call stack is exceeding because it gets trapped in a loop inside the postbuild/analyse visit function. I added a simple console.log to the id and it looks like this:
_remote-eooeoj.js
_app-server.js
_remote-12p15ba.js
_app-server.js
_app-server.js
_app-shared-server.js
_remote-qn5w1w.js
_app-server.js
_remote-eooeoj.js
_app-server.js
_remote-12p15ba.js
_app-server.js
_app-shared-server.js
_remote-qn5w1w.js
_app-server.js
_remote-eooeoj.js
_app-server.js
_remote-12p15ba.js
_app-server.js
_app-shared-server.js
_remote-qn5w1w.js
_app-server.js
_remote-eooeoj.js
The remote files have references like this:
12p15ba -> qn5w1w -> eooeoj -> 12p15ba
I added a very basic set to ensure ids are not run more than once and the build compiles and application runs. I don't think this is the correct fix as something else is causing the visit imports to circular reference, but its a simple quick fix that works. Incoming PR that illustrates this fix (albeit, not how to reproduce the problem yet).
I am experiencing this too in my project but with a slightly different error from version 2.39.0 and up to 2.42.1.
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:///<PROJECTPATH>/node_modules/.pnpm/@sveltejs+kit@2.42.1_@sveltejs+vite-plugin-svelte@6.1.0_svelte@5.38.10_vite@7.0.6_@type_daa69acea58dced3953c6ac836d08a26/node_modules/@sveltejs/kit/src/core/postbuild/prerender.js:498:27)
at async MessagePort.<anonymous> (file:///<PROJECTPATH>/node_modules/.pnpm/@sveltejs+kit@2.42.1_@sveltejs+vite-plugin-svelte@6.1.0_svelte@5.38.10_vite@7.0.6_@type_daa69acea58dced3953c6ac836d08a26/node_modules/@sveltejs/kit/src/utils/fork.js:23:16)
I can fix this by adding just the prerender import from $app/server in my .remote. files.
I am unsure about how it is related to the build issues mentioned here but wanted to bring it up in this context.
I am experiencing this too in my project but with a slightly different error from version 2.39.0 and up to 2.42.1.
Error:
node:internal/event_target:1101 process.nextTick(() => { throw err; }); ^ TypeError [Error]: Cannot convert undefined or null to object at Function.values (
) at prerender (file:/// /node_modules/.pnpm/@sveltejs+kit@2.42.1_@@5.38.10_vite@7.0.6_@type_daa69acea58dced3953c6ac836d08a26/node_modules/@sveltejs/kit/src/core/postbuild/prerender.js:498:27) at async MessagePort. (file:/// /node_modules/.pnpm/@sveltejs+kit@2.42.1_@@5.38.10_vite@7.0.6_@type_daa69acea58dced3953c6ac836d08a26/node_modules/@sveltejs/kit/src/utils/fork.js:23:16) I can fix this by adding just the prerenderimport from$app/serverin my.remote.files. I am unsure about how it is related to the build issues mentioned here but wanted to bring it up in this context.
I'm pretty certain the PR I made above won't address this, but I do think this is related, specifically to how sveltekit is chunking remote functions. In my project, for whatever reason, the remote functions get chunked into a doom loop. Where as yours get chunked in such a way that some variable becomes undefined.
#14450 was merged but I'll keep this open so we can try to get to the bottom of this issue
I have this problem too ( including in version 2.42.1 )
ReferenceError [Error]: Cannot access 'defaultString' before initialization
When i tried to bypass the problem with moving my schemas and helpers directly into the remote file i got this problem:
I've got good news and bad news. That fixes the
cannot access before initialization error. However it has been replaced with the following:node:internal/event_target:1101 process.nextTick(() => { throw err; }); ^ RangeError [Error]: Maximum call stack size exceeded at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:259:16) at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:271:5) at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:271:5) at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:271:5) at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:271:5) at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:271:5) at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:271:5) at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:271:5) at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:271:5) at visit (file:///Users/eitanmiller/Dev/platform/node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:271:5)also the sourcemap warning is still present (though admitantly unrelated)
I'm trying to build a minimal reproduction but can't reproduce it
I am experiencing this too in my project but with a slightly different error from version 2.39.0 and up to 2.42.1.
Error:
node:internal/event_target:1101 process.nextTick(() => { throw err; }); ^ TypeError [Error]: Cannot convert undefined or null to object at Function.values (
) at prerender (file:/// /node_modules/.pnpm/@sveltejs+kit@2.42.1_@@5.38.10_vite@7.0.6_@type_daa69acea58dced3953c6ac836d08a26/node_modules/@sveltejs/kit/src/core/postbuild/prerender.js:498:27) at async MessagePort. (file:/// /node_modules/.pnpm/@sveltejs+kit@2.42.1_@@5.38.10_vite@7.0.6_@type_daa69acea58dced3953c6ac836d08a26/node_modules/@sveltejs/kit/src/utils/fork.js:23:16) I can fix this by adding just the
prerenderimport from$app/serverin my.remote.files. I am unsure about how it is related to the build issues mentioned here but wanted to bring it up in this context.
Tried out version 2.42.2 but I still get this error when building.
I ran into the same issue — first with the Cannot access problem. I didn’t try 2.42.1, but I saw something about maximumstack there as well.
After digging into it, I realized this:
I don’t use a .env file. Instead, I have a /server/config.ts file:
export default {
redis_session: PROD_MODE ? {
port : 6379, // Production
host : 'redis-session',
maxRetriesPerRequest: 1
} : {
port : 46322, // Development
host : 'localhost',
maxRetriesPerRequest: 1
}
// ...
}
Since this file lives on the /server/ folder, it never leaks to the client side, so I’ve been using it without worry.
Later, when I saw the remote functions feature, I wanted to give it a try.
Of course, inside those remote functions I imported values from this config.ts file.
Everything was fine and builds worked until form validation became mandatory. When I tried building with 2.42.2, the problem appeared. As soon as I removed the config imports from the remote functions, the issue went away.
I’ll try to reproduce it again when I have time, but I wanted to share this in case it helps.
ok more investigation bug:
found that only happens with file my.custom.code.remote.ts
when i change file name to code.remote.ts bug fixes
I will provide full reproduce..
reproduced here: #14494
I am still not able to build my project when using another package with exported remote functions.
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)
The package project builds successfully with the new versions and exports them in an index.js file inside $lib:
// Services
export * from './services/collections.remote.js';
export * from './services/custom.remote.js';
Currently trying to work on a small reproduce for this.
Further information 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
...
This does ONLY happen if I DON'T import / use the exported remote function. If I just use it in my package. The error does not happen.
Please open a new issue with a reproduction
Pro tip: You can prefix GitHub URLs of issues, PRs or discussions with svcl.dev/ to view them on this page! Also try it on a GitHub release ;)