question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: Cannot find module '/var/task/src/app/lambda/puppeteer/lib/Browser

See original GitHub issue

Environment

Working great locally. I’ve created a lambda layer via:

git clone --depth=1 https://github.com/Sparticuz/chrome-aws-lambda.git cd chrome-aws-lambda make chrome_aws_lambda.zip

Uploaded the zip manually to a new layer via AWS console, and attached it (via serverless config below) to my lambda

I’m excluding the @sparticuz/** module during package/bundle so my lambda uses the layer version.

plugins:
  - serverless-esbuild

package:
  individually: true
  patterns:
    - '!node_modules/@sparticuz/**'

custom:
  esbuild:
    platform: node
    bundle: true
    external: ['knex', 'pg']

functions:
  api:
    handler: src/app/lambda/api.handler
    url: true
    layers:
      - arn:aws:lambda:eu-west-2:173514141284:layer:chrome-aws-lambda:2

and my function code is:

import chromium from '@sparticuz/chrome-aws-lambda'

browser = await chromium.puppeteer.launch({
  args: chromium.args,
  defaultViewport: chromium.defaultViewport,
  executablePath: await chromium.executablePath,
  headless: true,
  ignoreHTTPSErrors: true
})

but running it on lambda gives me a 500 with:

Error: Cannot find module '/var/task/src/app/lambda/puppeteer/lib/Browser'\nRequire stack:\n- /var/task/src/app/lambda/api.js\n- /var/runtime/index.mjs

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
alexcrooxcommented, Aug 3, 2022

OK finally got it working without bundling it in the lambda, but loading it from the layer;

const importPath = process.env.LAMBDA_TASK_ROOT
  ? '/opt/nodejs/node_modules/@sparticuz/chrome-aws-lambda/build/index.js'
  : '@sparticuz/chrome-aws-lambda'

const { default: chromium } = await import(importPath)

the build/index.js path is required for me because I’m using ESM and Lambda layers don’t support native ESM loading yet

package:
  individually: true
  patterns:
    - '!@sparticuz/**'
1reaction
alexcrooxcommented, Aug 3, 2022

OK the package exclude idea wasn’t the solution, using esbuild you need to mark this entire lib as external:

custom:
  esbuild:
    platform: node
    bundle: true
    external: ['@sparticuz/chrome-aws-lambda']

EDIT: Spoke too soon, this bundles the binaries which makes for a huge package. I’ll keep trying to get it to use the lambda layer version

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I resolve "Cannot find module" error using Node.js?
Using npm install installs the module into the current directory only (in a subdirectory called node_modules ). Is app.js located under ...
Read more >
cannot find module [Node npm Error Solved] - freeCodeCamp
In my case, I got it like this "Module not found: Error: Can't resolve 'react-icons/fa' in 'C:\Users\user\Desktop\Projects\Address Locator\ ...
Read more >
ServiceNow Community servicenow community
I am trying to setup the CLI for component development and get the infamous error "keytar module" not found. Can someone tell me...
Read more >
Fix Global Installs Not Working | "Cannot find module" error FIX
Getting " Cannot find module " after installing something globally (with -g)? Well, this video shows you how to fix global package/module ...
Read more >
Error: Cannot find module 'bcrypto.node' · Issue #127 - GitHub
i got some error here D:\DataGalang\test-cepot\hs-airdrop\node_modules\loady\lib\loady.js:156 throw err; ^ Error: Cannot find module ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found