Serverless function results in 502 error when importing JSDOM library

Hello! I’ve been using Netlify functions for a while and have had minimal problems, but this one I can’t figure out. I’ve tried to recreate it in a minimal capacity. The basic problem is any time I import the jsdom library in a function I get an error. All of the details are below, but I’m stuck!

Some notes:

  • This works locally with netlify dev
  • If you comment out the import line this function works fine live on Netlify (as well as locally).
  • I have other functions that import Node libraries, making me think it’s something with the jsdom library, but I have no idea what.
  • I have tried solutions from other support posts and I still receive the error. (Although my error is different from theirs.)
    • In particular, I added “jsdom” to my external_node_modules

Here’s my site:

You can hit this function from the JS console using:

await fetch('/.netlify/functions/test-jsdom')

Here’s the function code:

import { JSDOM } from 'jsdom'
export default async function handler(req, context) {
    return new Response('this is a jsdom test', { status: 200 })
}

The library (“jsdom”) is in my package.json (and lock file):

{
  ...other stuff,
  "dependencies": {
    ...other stuff,
    "jsdom": "^27.2.0"
  }
}

And here’s the relevant portion of my netlify.toml:

[functions]
  directory = "api/functions"
  node_bundler = "esbuild"
  external_node_modules = ["jsdom"]

And here are the errors from the Netlify Function log (I’ve cleaned it up a bit for readability):

Dec 2, 10:34:05 AM: ERROR  Uncaught Exception 	{
    "errorType":"TypeError",
    "errorMessage":"Cannot read properties of undefined (reading 'get')",
    "stack":[
        "TypeError: Cannot read properties of undefined (reading 'get')",
        "    at Object.<anonymous> (/var/task/api/node_modules/webidl-conversions/lib/index.js:325:94)",
        "    at Module._compile (node:internal/modules/cjs/loader:1364:14)",
        "    at Module._extensions..js (node:internal/modules/cjs/loader:1422:10)",
        "    at Module.load (node:internal/modules/cjs/loader:1203:32)",
        "    at Module._load (node:internal/modules/cjs/loader:1019:12)",
        "    at Module.require (node:internal/modules/cjs/loader:1231:19)",
        "    at require (node:internal/modules/helpers:177:18)",
        "    at Object.<anonymous> (/var/task/api/node_modules/whatwg-url/lib/URL.js:3:21)",
        "    at Module._compile (node:internal/modules/cjs/loader:1364:14)",
        "    at Module._extensions..js (node:internal/modules/cjs/loader:1422:10)"
    ]}
Dec 2, 10:34:05 AM: ERROR  Unhandled Promise Rejection 	{
    "errorType":"Runtime.UnhandledPromiseRejection",
    "errorMessage":"TypeError: Cannot read properties of undefined (reading 'get')",
    "reason":{
        "errorType":"TypeError",
        "errorMessage":"Cannot read properties of undefined (reading 'get')",
        "stack":["((repeat of stack above))"]
    },
    "promise":{},
    "stack":[
        "Runtime.UnhandledPromiseRejection: TypeError: Cannot read properties of undefined (reading 'get')",
        "    at process.<anonymous> (file:///var/runtime/index.mjs:1448:17)",
        "    at process.emit (node:events:517:28)",
        "    at emit (node:internal/process/promises:149:20)",
        "    at processPromiseRejections (node:internal/process/promises:283:27)",
        "    at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"
    ]}
Dec 2, 10:34:05 AM: INIT_REPORT Init Duration: 457.52 ms	Phase: init	Status: error	Error Type: Runtime.Unknown

https://netlify.zendesk.com/agent/tickets/478028

Hey @jakerella :wave:,
Thanks for reaching out!

We’ve gone ahead and created a support ticket for you, so our team can follow up with you directly via email from the help desk. Our Support crew will be in touch with you by email soon.

Great news: these days anyone can reach out to Netlify Support. First, you can try getting an answer using Ask Netlify, our helpful AI search tool. If your question isn’t answered there, you can submit a ticket using the support form, and we’ll take it from there.

We’re keeping the community around for swapping ideas, sharing tips and tricks, and talking shop with other folks building on the platform — but for support issues, tickets are the way to go.

Thanks for being here, and keep an eye out for that email from us!

1 Like