SvelteKit + Supabase deployment error

I’ve been pulling my hair out over this error. I’m not sure what I’m doing wrong or where what exactly I need to fix. Is the issue with the supabaseUrl saying it’s required? Everything works locally just fine.

Changing “npm run build” to “CI= npm run build” doesn’t appear to help either.

Can anyone help me?

Here’s the deployment log.

4:14:05 PM: Build ready to start
4:14:09 PM: build-image version: 122b31996ccaffd45d820a452d6227f8312110cc (focal)
4:14:09 PM: build-image tag: v4.5.3
4:14:09 PM: buildbot version: 93aa77af0f8c482a6dc06593f94c9bd96f918676
4:14:09 PM: Fetching cached dependencies
4:14:09 PM: Failed to fetch cache, continuing with build
4:14:09 PM: Starting to prepare the repo for build
4:14:10 PM: No cached dependencies found. Cloning fresh repo
4:14:10 PM: git clone https://github.com/roberohn/my-todo-app
4:14:10 PM: Preparing Git Reference refs/heads/main
4:14:11 PM: Parsing package.json dependencies
4:14:12 PM: Starting build script
4:14:12 PM: Installing dependencies
4:14:12 PM: Python version set to 2.7
4:14:12 PM: Downloading and installing node v16.14.0...
4:14:12 PM: Downloading https://nodejs.org/dist/v16.14.0/node-v16.14.0-linux-x64.tar.xz...
4:14:13 PM: Computing checksum with sha256sum
4:14:13 PM: Checksums matched!
4:14:16 PM: Now using node v16.14.0 (npm v8.3.1)
4:14:16 PM: Started restoring cached build plugins
4:14:16 PM: Finished restoring cached build plugins
4:14:16 PM: Attempting ruby version 2.7.2, read from environment
4:14:17 PM: Using ruby version 2.7.2
4:14:18 PM: Using PHP version 8.0
4:14:18 PM: Started restoring cached node modules
4:14:18 PM: Finished restoring cached node modules
4:14:18 PM: Installing NPM modules using NPM version 8.3.1
4:14:25 PM: added 244 packages, and audited 245 packages in 6s
4:14:25 PM: 32 packages are looking for funding
4:14:25 PM:   run `npm fund` for details
4:14:25 PM: found 0 vulnerabilities
4:14:25 PM: NPM modules installed
4:14:26 PM: Started restoring cached go cache
4:14:26 PM: Finished restoring cached go cache
4:14:26 PM: go version go1.16.5 linux/amd64
4:14:26 PM: go version go1.16.5 linux/amd64
4:14:26 PM: Installing missing commands
4:14:26 PM: Verify run directory
4:14:28 PM: ​
4:14:28 PM: ────────────────────────────────────────────────────────────────
4:14:28 PM:   Netlify Build                                                 
4:14:28 PM: ────────────────────────────────────────────────────────────────
4:14:28 PM: ​
4:14:28 PM: ❯ Version
4:14:28 PM:   @netlify/build 26.3.5
4:14:28 PM: ​
4:14:28 PM: ❯ Flags
4:14:28 PM:   baseRelDir: true
4:14:28 PM:   buildId: 62150bcc572d43000734800d
4:14:28 PM:   deployId: 62150bcc572d43000734800f
4:14:28 PM: ​
4:14:28 PM: ❯ Current directory
4:14:28 PM:   /opt/build/repo
4:14:28 PM: ​
4:14:28 PM: ❯ Config file
4:14:28 PM:   /opt/build/repo/netlify.toml
4:14:28 PM: ​
4:14:28 PM: ❯ Context
4:14:28 PM:   production
4:14:28 PM: ​
4:14:28 PM: ────────────────────────────────────────────────────────────────
4:14:28 PM:   1. build.command from netlify.toml                            
4:14:28 PM: ────────────────────────────────────────────────────────────────
4:14:28 PM: ​
4:14:28 PM: $ npm run build
4:14:28 PM: > my-todo-app@0.0.1 build
4:14:28 PM: > svelte-kit build
4:14:28 PM: vite v2.8.0 building for production...
4:14:28 PM: transforming...
4:14:31 PM: ✓ 74 modules transformed.
4:14:31 PM: rendering chunks...
4:14:31 PM: .svelte-kit/output/client/_app/manifest.json                               1.36 KiB
4:14:31 PM: .svelte-kit/output/client/_app/pages/__layout.svelte-f4b1e392.js           4.26 KiB / gzip: 1.91 KiB
4:14:31 PM: .svelte-kit/output/client/_app/error.svelte-b3dfc185.js                    1.56 KiB / gzip: 0.75 KiB
4:14:31 PM: .svelte-kit/output/client/_app/pages/index.svelte-e237e8f0.js              4.30 KiB / gzip: 1.90 KiB
4:14:31 PM: .svelte-kit/output/client/_app/chunks/todoStore-28fc74c3.js                0.80 KiB / gzip: 0.43 KiB
4:14:31 PM: .svelte-kit/output/client/_app/start-5707cb7f.js                           22.81 KiB / gzip: 7.81 KiB
4:14:31 PM: .svelte-kit/output/client/_app/assets/pages/__layout.svelte-b2cadc54.css   6.02 KiB / gzip: 2.04 KiB
4:14:31 PM: .svelte-kit/output/client/_app/chunks/vendor-a32da090.js                   76.08 KiB / gzip: 20.93 KiB
4:14:31 PM: vite v2.8.0 building SSR bundle for production...
4:14:31 PM: transforming...
4:14:32 PM: ✓ 20 modules transformed.
4:14:32 PM: rendering chunks...
4:14:32 PM: .svelte-kit/output/server/manifest.json                      1.10 KiB
4:14:32 PM: .svelte-kit/output/server/app.js                             62.19 KiB
4:14:32 PM: .svelte-kit/output/server/entries/pages/__layout.svelte.js   2.02 KiB
4:14:32 PM: .svelte-kit/output/server/entries/pages/error.svelte.js      0.72 KiB
4:14:32 PM: .svelte-kit/output/server/entries/pages/index.svelte.js      2.26 KiB
4:14:32 PM: .svelte-kit/output/server/chunks/index-489d52f8.js           3.48 KiB
4:14:32 PM: .svelte-kit/output/server/chunks/todoStore-21855630.js       1.63 KiB
4:14:32 PM: Run npm run preview to preview your production build locally.
4:14:32 PM: 
4:14:32 PM: > Using @sveltejs/adapter-netlify
4:14:32 PM: Error: supabaseUrl is required.
4:14:32 PM:     at new SupabaseClient (/opt/build/repo/node_modules/@supabase/supabase-js/dist/main/SupabaseClient.js:50:19)
4:14:32 PM:     at createClient (/opt/build/repo/node_modules/@supabase/supabase-js/dist/main/index.js:25:12)
4:14:32 PM:     at file:///opt/build/repo/.svelte-kit/output/server/chunks/todoStore-21855630.js:5:18
4:14:32 PM:     at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
4:14:32 PM:     at async Promise.all (index 0)
4:14:32 PM:     at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
4:14:32 PM:     at async Promise.all (index 0)
4:14:32 PM:     at async respond$1 (file:///opt/build/repo/.svelte-kit/output/server/app.js:1446:13)
4:14:32 PM:     at async render_page (file:///opt/build/repo/.svelte-kit/output/server/app.js:1616:20)
4:14:32 PM:     at async resolve (file:///opt/build/repo/.svelte-kit/output/server/app.js:1784:105)
4:14:32 PM: Error: supabaseUrl is required.
4:14:32 PM: > 500 /
4:14:32 PM:     at file:///opt/build/repo/node_modules/@sveltejs/kit/dist/chunks/index5.js:409:11
4:14:32 PM:     at visit (file:///opt/build/repo/node_modules/@sveltejs/kit/dist/chunks/index5.js:583:5)
4:14:32 PM:     at processTicksAndRejections (node:internal/process/task_queues:96:5)
4:14:32 PM: ​
4:14:32 PM: ────────────────────────────────────────────────────────────────
4:14:32 PM:   "build.command" failed                                        
4:14:32 PM: ────────────────────────────────────────────────────────────────
4:14:32 PM: ​
4:14:32 PM:   Error message
4:14:32 PM:   Command failed with exit code 1: npm run build
4:14:32 PM: ​
4:14:32 PM:   Error location
4:14:32 PM:   In build.command from netlify.toml:
4:14:32 PM:   npm run build
4:14:32 PM: ​
4:14:32 PM:   Resolved config
4:14:32 PM:   build:
4:14:32 PM:     command: npm run build
4:14:32 PM:     commandOrigin: config
4:14:32 PM:     publish: /opt/build/repo/build
4:14:32 PM:     publishOrigin: config
4:14:32 PM: Caching artifacts
4:14:32 PM: Started saving node modules
4:14:32 PM: Finished saving node modules
4:14:32 PM: Started saving build plugins
4:14:32 PM: Finished saving build plugins
4:14:32 PM: Started saving pip cache
4:14:32 PM: Finished saving pip cache
4:14:32 PM: Started saving emacs cask dependencies
4:14:32 PM: Finished saving emacs cask dependencies
4:14:32 PM: Started saving maven dependencies
4:14:32 PM: Finished saving maven dependencies
4:14:32 PM: Started saving boot dependencies
4:14:32 PM: Finished saving boot dependencies
4:14:32 PM: Started saving rust rustup cache
4:14:32 PM: Finished saving rust rustup cache
4:14:32 PM: Started saving go dependencies
4:14:32 PM: Finished saving go dependencies
4:14:35 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
4:14:35 PM: Creating deploy upload records
4:14:35 PM: Failing build: Failed to build site
4:14:35 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2
4:14:35 PM: Finished processing build request in 26.153877959s

Hey @roberohn

So when you set up Supabase you have

import { createClient } from '@supabase/supabase-js'

const supabase = createClient(
  "https://lkjaslkdjas.supabase.co",    // supabaseUrl
  "eySome really long key string..."
);

What the error is saying is the URL isn’t present. Have you by any chance used environment variables in the configuration above e.g

const supabase = createClient(
  process.env.SUPABASE_URL,
  process.env.SUPABASE_KEY,
);

And if so, have you added those environment variables via the Netlify UI?

Hi @coelmay

Thank you for your reply, I had missed adding the environment variables to Netlify UI. That’s now added :+1:

My Supabase setup is similar to the example you’ve provided. Here’s what I have which works locally.

import { createClient } from "@supabase/supabase-js";

const supabaseUrl = import.meta.env.SUPABASE_URL;
const supabaseAnonKey = import.meta.env.SUPABASE_ANON_KEY;

export const supabase = createClient(supabaseUrl, supabaseAnonKey);

I’ve added SUPABASE_URL and SUPABASE_ANON_KEY to Netlify UI and sadly I’m continuing to get the same original error when I’ve re-run the build.

If you console.log() these variables as part of the build, what do you see?

What if you change import.meta.env to process.env?

I’ve just solved it by prefixing VITE_PUBLIC_ infront of my environment variables!

Thanks for your help @coelmay.

2 Likes

Great @roberohn

Was going to point you to the environment variables section of the sveltekit faq that mentions that :slight_smile:

1 Like