Functions: runtime error with require function


I have a dead simple netlify function that I use for a webhook (send an email with a link to download a file), it worked fine until today. The build step passes just fine, everything is green.

It raises an error on calling deployed function on any require function call (jsonwebtoken lib, ../localfile.js), while it works fine localy using netlify-cli.

I have tried

  • updating to node 20
  • updating every libraries

here are my dependencies (mixing build, local dev, and functions deps):

  "dependencies": {
    "@aws-sdk/client-s3": "^3.490.0",
    "@aws-sdk/s3-request-presigner": "^3.490.0",
    "@sendgrid/mail": "^8.1.0",
    "async-file": "^2.0.2",
    "chokidar": "^3.0.0",
    "connect": "^3.7.0",
    "html-to-text": "^9.0.5",
    "jsonwebtoken": "^9.0.0",
    "marked": "^4.2.2",
    "netlify-cli": "^17.14.0",
    "npm-run": "^5.0.1",
    "pug": "^3.0.2",
    "serve": "^14.1.1",
    "serve-static": "^1.14.1",
    "stylus": "^0.61.0",
    "yaml-front-matter": "^4.0.0"

(I’m not using aws s3, but an s3 like api on Scaleway for the files to send)

the require calls:

const jwt = require('jsonwebtoken');
const tpl = require('../emailTemplate.js');
const sendgrid = require('@sendgrid/mail');
const htmlToText = require('html-to-text').htmlToText;

the error in the logs:

Jan 15, 08:50:27 PM: f8e8058f 2024-01-15T19:48:15.451Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'jsonwebtoken'\nRequire stack:\n- /var/task/function.js\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'jsonwebtoken'","Require stack:","- /var/task/function.js","- /var/runtime/index.mjs","    at _loadUserApp (file:///var/runtime/index.mjs:1087:17)","    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)","    at async start (file:///var/runtime/index.mjs:1282:23)","    at async file:///var/runtime/index.mjs:1288:1"]}

(when const tpl = require('../emailTemplate.js'); is the first require, it’s the one that breaks)

I’ve run out ouf ideas to work around this problem, I’ve seen many posts with the same error but most of them use nextjs which I’m not.

I hope someone will be able to suggest an easy solution,


Have you tried adding:

  external_node_modules = ["jsonwebtoken"]

to your netlify.toml?

hello !

I tried that, but didn’t worked.

But I’ve found a solution, renaming my files with the .ts extension (I guess the esbuild pass is linking everything better).

Thanks for your help, this is now resolved :slight_smile: