Netlify - Cannot find module in Functions

Hello all,

I’ve been struggling with this one for the last few days, scouring the forums but have been turning up blank.

I’m attempting to deploy my Functions to Netlify and the deployment completed successfully over there. However, when I attempt to hit the API endpoint I get the following error:

"message": "[error] evaluation failed with error: Cannot find module '/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Browser'"

This error does not exist for me locally. I have confirmed that Browser.ts does exist in node_modules. I have attempted including package.json files individually in my functions. I have tried different node_bundler settings in my netlify.toml. I have removed other dependencies from the api/package.json deps (chrome-aws-lambda) and the error has changed; then throwing an error for the newly missing dependency. This indicates to me that for some reason puppeteer-core is not being bundled, and I cannot figure out why.

Here’s my netlify.toml:

[build]
command = "yarn rw deploy netlify"
publish = "web/dist"
functions = "api/dist/functions"

[dev]
  framework = "redwoodjs"
  targetPort = 8910
  port = 8888

[[redirects]]
  from = "/*"
  to = "/200.html"
  status = 200

[functions]
  node_bundler = "esbuild"
  external_node_modules = [
    "chrome-aws-lambda",
    "puppeteer-core",
    "@magic-sdk/admin",
    "@clerk/clerk-sdk-node/instance",
    "firebase-admin",
    "import-fresh"
    ]

I am not using the Netlify CLI to deploy. I am running yarn rw deploy netlify to confirm the build, then pushing it to my repo and letting Netlify do the rest.

Here is my api/package.json:

{
  "name": "api",
  "version": "0.0.0",
  "private": true,
  "devDependencies": {
    "@types/puppeteer": "^5.4.6",
    "puppeteer": "^19.2.2"
  },
  "dependencies": {
    "@redwoodjs/api": "2.2.3",
    "@redwoodjs/graphql-server": "2.2.3",
    "chrome-aws-lambda": "^10.1.0",
    "puppeteer-core": "^19.2.2"
  }
}

I’m completely stuck at the moment. I’d rather not switch away from Netlify is possible. It seems to me that something in how Redwood is preparing the /dist may be at fault, but I feel like I’m grasping at straws. Any guidance anyone could provide would be much appreciated.

It appears from this video that what you are trying to do is possible Automate Chrome in an API with Puppeteer and Netlify Serverless Functions - YouTube

But something may have changed with Netlify.

Have you tried following the above video and deploying Puppeteer to Netlify without using Redwood?

Hi @jonparker ,

Thank you for your reply! I hadn’t seen that video specifically, but it actually helped push me in the right direction. This wasn’t an issue with Redwood or Netlify; it’s related to how I was trying to import puppeteer-core via chrome-aws-lambda (I failed to paste that file in here). However, this led me down a black hole of other Netlify-related issues with this dependency combination.

It was enough of a pain that I actually wrote up a Medium article for those running into the same issues as me. For anyone interested you can view that here: Deploy Your Puppeteer Function to Netlify Since Node 16 | by Stephen Dimock | Nov, 2022 | Medium

That said, I can report back that now that I’ve resolved these issues things are bundling perfectly with Redwood and have deployed without issue on Netlify!

Thanks again for the help!

Steve

1 Like