Sudden failure to import zod. (sveltekit)

After building fine many times suddenly node is unable to find the validator library I’ve been using just fine for months, zod.
I first got this error:

error during build:
[vite]: Rollup failed to resolve import “zod” from “/vercel/path0/src/lib/formSchemas/schemas.ts”.
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
build.rollupOptions.external

Error: Command “npm run build” exited with 1

So now my vite.config.ts looks like this:

import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

export default defineConfig({
	plugins: [sveltekit()],
	build:{
		rollupOptions:{
			external: [
				'zod'
			]
		}
	}
});

And now I get this error

node:internal/event_target:1105
process.nextTick(() => { throw err; });
^
Error: Cannot find package ‘/vercel/path0/node_modules/zod/index.js’ imported from /vercel/path0/.svelte-kit/output/server/chunks/schemas.js

My app builds fine on my machine. After this error started happening I updated all my packages and that hasn’t fixed or changed the issue. I had not changed anything to do with importing zod when the error appeared. My current production page uses the exact same library and imports with no issue.

1 Like

my svelte.config.js:

import adapter from '@sveltejs/adapter-vercel'
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: {
		// 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(),
		// for hosting on localhost via ngrok 
		csrf: {
			checkOrigin: false
		}
	}
};

export default config;

Having changed nothing about how zod is imported or anything else having to do with build configs, just adding a few pages that do not reference zod at all in any way, my latest build was successful. I do not know what changed to cause the issues and I do not know what changed to fix the issue. I guess hopefully it won’t happen again

Again having changed nothing about zod it’s now not building again

Sorry about this. Sounds frustrating! Could you share a minimal reproducible example with us?

1 Like

I don’t know how to make a minimal reproduction when I don’t know what is broken. Can I link to builds that didn’t build vs builds that did?
This commit didn’t build
This commit did build

Have you tried doing a manual chunk for zod instead?

build: {
    rollupOptions: {
        output: {
            manualChunks: {
                zod: ['zod']
            }
        }
    }
}

If I remember correctly, externalizing a package in Vite will exclude it from the bundle.
Excluding zod from the bundle means it won’t work client side (at least, not without some workarounds) and will also require that zod is a runtime dependency (not a devDepenency).