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

19

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(
Sign up to request clarification or add additional context in comments.

4 Comments

If you import it that way can you still use ”export const weeklyReminder” if not not do you export the function
Also using vanilla JavaScript
No you'd use: exports.weeklyReminder = functions.pubsub.schedule('every Wednesday 21:00').onRun(async context =>{
@JasonByrne VS Code suggest to use ES module thus ended up with import { region } from 'firebase-functions';. Should we just ignore the suggestion?
-1

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

Comments

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.