supabase js - ES Code in Twilio Function (Console UI) fails with Unexpected token 'export' error. Code in Common

admin2025-04-21  1

This is the code using CommonJS that works:

    const { createClient } = require('@supabase/supabase-js');
    exports.handler = async (context, event, callback) => {
       const supabaseUrl = '';
       const supabase = createClient(supabaseUrl, context.SUPABASE_KEY);

This is the code using ESM that throws the error:


    export const handler = async (context, event, callback) => {
       const { createClient } = await import('@supabase/supabase-js');
       const supabaseUrl = '';
       const supabase = createClient(supabaseUrl, context.SUPABASE_KEY);

If I move the import out of my function then Twilio throws the same error

    const { createClient } = import('@supabase/supabase-js');  
    export const handler = async (context, event, callback) => {
      // const { createClient } = await import('@supabase/supabase-js');
      const supabaseUrl = '';
      const supabase = createClient(supabaseUrl, context.SUPABASE_KEY);

Twilio UI is using Node v18

I also have this code that does work and Twilio Docs say ESM is supported

exports.handler = async function (context, event, callback) { const twilioClient = context.getTwilioClient();

This is the code using CommonJS that works:

    const { createClient } = require('@supabase/supabase-js');
    exports.handler = async (context, event, callback) => {
       const supabaseUrl = 'https://teuytpiznwvvdhrkycqb.supabase.co';
       const supabase = createClient(supabaseUrl, context.SUPABASE_KEY);

This is the code using ESM that throws the error:


    export const handler = async (context, event, callback) => {
       const { createClient } = await import('@supabase/supabase-js');
       const supabaseUrl = 'https://teuytpiznwvvdhrkycqb.supabase.co';
       const supabase = createClient(supabaseUrl, context.SUPABASE_KEY);

If I move the import out of my function then Twilio throws the same error

    const { createClient } = import('@supabase/supabase-js');  
    export const handler = async (context, event, callback) => {
      // const { createClient } = await import('@supabase/supabase-js');
      const supabaseUrl = 'https://teuytpiznwvvdhrkycqb.supabase.co';
      const supabase = createClient(supabaseUrl, context.SUPABASE_KEY);

Twilio UI is using Node v18

I also have this code that does work and Twilio Docs say ESM is supported

exports.handler = async function (context, event, callback) { const twilioClient = context.getTwilioClient();

Share Improve this question asked Jan 22 at 20:20 Rick GirardinRick Girardin 11 silver badge1 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

Twilio functions only support CJS [1]. You can use dynamic imports, as seen in the snippet below

exports.handler = async (context, event, callback) => {
  const { createClient } = await import('@supabase/supabase-js');
  const supabaseUrl = 'https://abc123.supabase.co';
  const supabase = createClient(supabaseUrl, context.SUPABASE_KEY);  

You can still write ESM code locally; however, you will need to transform or bundle it as CJS before uploading it to Twilio Functions.

I suspect its not the answer you were hoping for but I hope it helps.

Rob

[1] https://www.twilio.com/docs/serverless/functions-assets/faq#how-can-i-use-an-es-module-in-my-function

转载请注明原文地址:http://anycun.com/QandA/1745229963a90520.html