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:

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.

1 Like

Hey Jen,
Sorry for the late reply but we’ve scrapped this feature so I no longer need help with the issue, thanks anyway! Just a quick follow-up question, is there any way to delete or edit this post? It seems that I am unable to and I’m unsure why (just want to get rid of the link).

Thanks in advance

Hey @lingat917,
No worries, I just removed the link!

And as you spend more time in the forum responding to people and creating posts, our system will grant you more permissions like editing- but for now, our team will have to take care of that kind of thing for you.

@jen This is exactly an issue i currently have using googleapis package. I don’t think i’ve seen a solution yet.

hi @mattbtay - any progress on this one? we have been a bit underwater this week but i wanted to check back in and see how you are doing with this.