Skip to content

Reproduce issue for: "remote function build issues sveltekit @ 2.39.0 and up (tested up to and including 2.42.0)" #14444 #14494

@tunailgaz

Description

@tunailgaz

We really need a minimum reproduction (or any kind of reproduction) in that case.

Originally posted by @dummdidumm in #14444
created new project with
npx sv create

package.json

{
	"name": "build-error-reproduce",
	"private": true,
	"version": "0.0.1",
	"type": "module",
	"scripts": {
		"dev": "vite dev",
		"build": "vite build",
		"preview": "vite preview",
		"prepare": "svelte-kit sync || echo ''",
		"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
		"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
	},
	"devDependencies": {
		"@sveltejs/adapter-node": "5.3.2",
		"@sveltejs/kit": "2.42.2",
		"@sveltejs/vite-plugin-svelte": "6.2.0",
		"@types/node": "22.14.1",
		"svelte": "5.39.3",
		"svelte-check": "4.3.1",
		"typescript": "5.9.2",
		"vite": "7.1.6"
	},
	"dependencies": {
		"ioredis": "^5.7.0"
	}
}

svelte.config.js

import adapter from '@sveltejs/adapter-node';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

/** @type {import('@sveltejs/kit').Config} */
const config = {
	// Consult https://svelte.dev/docs/kit/integrations
	// for more information about preprocessors
	preprocess: vitePreprocess(),

	kit: {
    experimental: {
      remoteFunctions: true
    },
		// adapter-auto only supports some environments, see https://svelte.dev/docs/kit/adapter-auto for a list.
		// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
		// See https://svelte.dev/docs/kit/adapters for more information about adapters.
		adapter: adapter(),
    version: {
      pollInterval: 120_000
    }
	}
};

export default config;

/src/lib/remotes/auth.remote.ts

import { query } from "$app/server";
import { redisSession } from "$lib/server/redis/connection";

export const getUserSession = query(async () => {
  const session = await redisSession.get('test');
  return { session }
})

/src/lib/remote/my.custom.remote.ts

import { form } from "$app/server"
import config from "$lib/server/env";
import { getUserSession } from "$lib/remotes/auth.remote";

export const testForm = form('unchecked', async () => {
  const session = await getUserSession();
  console.log(`config: ${JSON.stringify(config)}`);
  return { success: session }
})

/src/server/redis/connection.ts

import config from '$lib/server/env';
import Redis from 'ioredis';
import { building } from '$app/environment';

const redisSession = new Redis(config.redis_session);


redisSession.on('connect', () => {
  if (building) return;
  console.info(`Redis session connected ${config.redis_session.host}:${config.redis_session.port}`);
}).on('error', (err) => {
  if (building) return;
  console.warn(`Redis session ${config.redis_session.host}:${config.redis_session.port} Error: ${err}`);
});


const redisSessionHealthCheck = () => {
  return redisSession.ping('ok');
}

export {
  redisSession,
  redisSessionHealthCheck,
};

/src/server/env.ts

const NODE_ENV = process.env.NODE_ENV!
const PROD_MODE = NODE_ENV === 'production'
export default {
  redis_session: PROD_MODE ? {
    port                : 6379, // Production
    host                : 'redis-session',
    maxRetriesPerRequest: 1
  } : {
    port                : 46322,  // Development
    host                : 'localhost',
    maxRetriesPerRequest: 1
  },
};

finally /routes/+page.svelte

<script lang="ts">
    import { testForm } from "$lib/remotes/my.custom.remote";
</script>

<section class="flex flex-wrap w-full py-4">
    <form {...testForm} class=" my-2 py-2"></form>
</section>

when you run vite build
this error happens:

> [email protected] build
> vite build

vite v7.1.6 building SSR bundle for production...
✓ 181 modules transformed.

node:internal/event_target:1101
  process.nextTick(() => { throw err; });
                           ^
ReferenceError [Error]: Cannot access 'config' before initialization
    at /.svelte-kit/output/server/chunks/remote-4cjdf.js:13:32
    at ModuleJob.run (node:internal/modules/esm/module_job:329:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:644:26)
    at async analyse (node_modules/@sveltejs/kit/src/core/postbuild/analyse.js:173:34)
    at async MessagePort.<anonymous> (node_modules/@sveltejs/kit/src/utils/fork.js:23:16)

Node.js v22.17.1

when you change my.custom.remote.ts file name to custom.remote.ts error doesnt show anymore

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions