cdnsupabase

Error:createClient is not defined When using supabase cdn


I'm using Supabase CDN's for a project. When I call the createClient() function this error comes up:

createClient is not defined

I tried it with an older version of the CDN, with another browser and using the old method of SupabaseClient.createClient, but I got:

SupabaseClient is not defined

Is the CDN not working correctly or something else?

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title>HTML</title>

  <!-- Custom Styles -->
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <p>Nothing to see here...</p>

  <!-- Project -->

  <script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2"></script>

  <script src="main.js"></script>
</body>
</html>
const SupabaseKey = // api here
const SupabaseUrl =// URL here

const options = {
  db: {
    schema: 'public',
  },
  auth: {
    autoRefreshToken: true,
    persistSession: true,
    detectSessionInUrl: true
  }
}

const supabase = createClient(SupabaseUrl, SupabaseKey, options)

Solution

  • I fixed it by adding an import statement in the JS file like this:

    import createClient from 'https://cdn.jsdelivr.net/npm/@supabase/supabase-js/+esm'
    

    The +esm at the end of the URL is from universal module and it now works.