17

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@gmail.com',
        ...
    };
    return sgMail.send(msg);

}); 

How do you import into firebase functions?

Chris Evans
  • 845
  • 1
  • 13
  • 29

2 Answers2

16

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(
Chris Evans
  • 845
  • 1
  • 13
  • 29
Jason Byrne
  • 1,579
  • 9
  • 19
-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
Jeremy Bader
  • 382
  • 1
  • 6
  • 14