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