0

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();

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Coming from being a new Node developer I’m learning some of the limitations of using Twilio Function UI. Just tried to use Got and had to back that dependency to 11 to get Twilio to build it. Thanks for answer and sample code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.