20

I'm, having an issue with my Firebase function. I'm getting the below error.

SyntaxError: Cannot use import statement outside a module

below is my code:

import * as functions from 'firebase-functions';
import * as sgMail from '@sendgrid/mail';

sgMail.setApiKey(key);

export const weeklyReminder = functions.pubsub.schedule('every Wednesday 21:00').onRun(async context =>{

    const msg = {
        to: '[email protected]',
        ...
    };
    return sgMail.send(msg);

}); 

How do you import into firebase functions?

2 Answers 2

18

Are you using TypeScript or vanilla JavaScript? With plain JavaScript you'd use require like this:

const functions = require('firebase-functions');

Also, change the function to be the same as the below:

exports.weeklyReminder = functions.pubsub.schedule('every Thursday 21:00').onRun(
4
  • 1
    If you import it that way can you still use ”export const weeklyReminder” if not not do you export the function Commented Jan 16, 2020 at 7:00
  • 1
    Also using vanilla JavaScript Commented Jan 16, 2020 at 7:03
  • 1
    No you'd use: exports.weeklyReminder = functions.pubsub.schedule('every Wednesday 21:00').onRun(async context =>{ Commented Jan 16, 2020 at 17:22
  • @JasonByrne VS Code suggest to use ES module thus ended up with import { region } from 'firebase-functions';. Should we just ignore the suggestion? Commented Jul 29, 2022 at 20:48
-2

For me, I needed curly brackets around my instance, like so:

const { functions } = require('firebase-functions');

Otherwise I would get

TypeError: functions is not a function

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.