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
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.
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
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
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).