Runtime.ImportModuleError in Serverless Function

Hello,
I am trying to use serverless functions to autogenerate some certificate images. I have run into the following error:

    {
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module '@sendgrid/mail'\nRequire stack:\n- /var/task/src/helpers/generateCertificate.js\n- /var/task/src/sendCertificates.js\n- /var/task/sendCertificates.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module '@sendgrid/mail'",
    "Require stack:",
    "- /var/task/src/helpers/generateCertificate.js",
    "- /var/task/src/sendCertificates.js",
    "- /var/task/sendCertificates.js",
    "- /var/runtime/UserFunction.js",
    "- /var/runtime/index.js",
    "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:1015:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)",
    "    at Module.load (internal/modules/cjs/loader.js:879:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:724:14)",
    "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)",
    "    at internal/main/run_main_module.js:17:47"
  ]
}

The reason this bug is so strange is that in another serverless function (under the same site name) I have used the sendgrid module with no issues, how can it stop recognizing it in one function and not the other?

Here is how I am requiring the modules in the working function:

        const sgMail = require("@sendgrid/mail");
        var moment = require('moment-timezone');
        const getRegistrations = require('./getRegistrations');
        const formattedReturn = require('./formattedReturn');
        const formattedError = require('./formattedError');

Here is how I am requiring the code in the function that is not working:

    const sgMail = require("@sendgrid/mail");
    var moment = require("moment-timezone");
    const formattedReturn = require('./formattedReturn');
    const getRegistrations = require("./getRegistrations");
    const { createCanvas, loadImage } = require("canvas");
    require("dotenv").config();

The issue isn’t just for sendgrid, however, it is any of those modules that I decide to place at the top of the code (canvas, dotenv, moment-timezone).

Here is the package.json file:

Here is the serverless function that isn’t working:
https://sendcertificates--brave-davinci-f9bd5c.netlify.app/.netlify/functions/sendCertificates

Solutions that I have attempted and that have not worked:

  • Deleting node_modules from the repo
  • Adding .default at the end of all the requires (require(“canvas”).default)
  • Using CI= npm install as the build command

Any help would be appreciated :slight_smile:

Hey there,
Apologies for the slow response here. That’s very mysterious! I’m wondering if there’s a syntax error somewhere else in the file that’s causing that? Aside from that, I guess there could be a function bundling issue, but I don’t know why one function would be bundled differently from the other…

Are you still running into this? When I check your site, I’m not seeing a function with that name, so if its name has changed please let us know so we can dig in.