shelfio / chrome-aws-lambda-layer

58 MB Google Chrome to fit inside AWS Lambda Layer compressed with Brotli
MIT License
637 stars 45 forks source link

Cannot find module '/var/task/puppeteer/lib/Browser #29

Closed imikemiller closed 2 years ago

imikemiller commented 3 years ago

When running my code in the Lambda context I am getting the following error. Could anyone point me in the right direction to resolve this?

My code runs as expected when I execute it with jest with the full version of puppeteer installed.

Many thanks in advance!

{
  "errorType": "Error",
  "errorMessage": "Cannot find module '/var/task/puppeteer/lib/Browser'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
  "trace": [
    "Error: Cannot find module '/var/task/puppeteer/lib/Browser'",
    "Require stack:",
    "- /var/task/index.js",
    "- /var/runtime/UserFunction.js",
    "- /var/runtime/index.js",
    "    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:831:15)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:687:27)",
    "    at Module.require (internal/modules/cjs/loader.js:903:19)",
    "    at newRequire (/var/task/index.js:55:18)",
    "    at newRequire (/var/task/index.js:42:18)",
    "    at localRequire (/var/task/index.js:80:14)",
    "    at Function.get puppeteer [as puppeteer] (/var/task/index.js:391:7)",
    "    at Runtime.handler (/var/task/index.js:169:46)",
    "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
  ]
}
ghost commented 3 years ago

might be related to https://github.com/alixaxel/chrome-aws-lambda/issues/154

imikemiller commented 3 years ago

@chanthafef I moved to version 10 of the Lambda layer (arn:aws:lambda:eu-west-2:764866452798:layer:chrome-aws-lambda:10) and it works fine.

frankIT commented 3 years ago

I have the same issue. I first tryied the last ARN available (arn:aws:lambda:us-east-1:764866452798:layer:chrome-aws-lambda:22) with chrome-aws-lambda@8.0.2 and puppeteer-core@8.0 but it gave me the error quoted in this issue. I then noticed from the readme.md that that ARN (which should bundle chrome binary v86.0.4240.0) was supposed to work with chrome-aws-lambda@5.3.0, and so puppeteer-core@5.3.* So I downgraded those 2 packages but nothing changed. Reading this issue I gave a shot to the ARN layers v. 22, 20, 10. Nothing changed, I keep on getting: Cannot find module '/var/task/puppeteer/lib/Browser' Anyone can point me to the right direction to fix this? Thanks.

Minminzei commented 2 years ago

same error. any solution?

avnersch commented 2 years ago

+1 same error

up1io commented 2 years ago

Are there any solutions? I have the same error with

const layerArn = "arn:aws:lambda:us-east-1:764866452798:layer:chrome-aws-lambda:25";

"chrome-aws-lambda": "^10.1.0", "puppeteer-core": "13.4.1",

tomilaurell commented 2 years ago

I was building the project with aws-cdk and solved the problem by adding this part in the lambda build conf:

bundling: {
  externalModules: ['chrome-aws-lambda'],
  sourceMap: true
}

If you do not define chrome-aws-lambda as external modules, it does not work.

One way to prove this is to manually copy and paste code in aws console UI. It will work there. The problem at least in my case was how the lambda was packaged.

nerdmax commented 2 years ago

Are there any solutions? I have the same error with

const layerArn = "arn:aws:lambda:us-east-1:764866452798:layer:chrome-aws-lambda:25";

"chrome-aws-lambda": "^10.1.0", "puppeteer-core": "13.4.1",

I had the same error with these versions. Upgrading to puppeteer@13.7.0 and puppeteer-core@13.7.0 works.

DavidVanDrei commented 2 years ago

I'm having this problem on the current version 27 but if I switch to 25 it works. so use "arn:aws:lambda:us-east-1:764866452798:layer:chrome-aws-lambda:25";

vladholubiev commented 2 years ago

@DavidVanDrei could you please try v28 that I've just published?

DavidVanDrei commented 2 years ago

I Just tried the last few that have been updated. Everything past 26 does not work with my setup. My runtime is Node14 and Architecture is x86_64 if that matters. The error I'm getting is Error: Cannot find module 'chrome-aws-lambda' too so not exactly this issue

vladholubiev commented 2 years ago

@DavidVanDrei Try v29 and instead of chrome-aws-lambda you should now require in your code @sparticuz/chrome-aws-lambda

As chrome-aws-lambda was abandoned by the author, and this is the living fork for now

DavidVanDrei commented 2 years ago

@vladgolubev that works! Thanks for the help and for maintaining this

oscarbatlle commented 2 years ago

@DavidVanDrei Try v29 and instead of chrome-aws-lambda you should now require in your code @sparticuz/chrome-aws-lambda

As chrome-aws-lambda was abandoned by the author, and this is the living fork for now

give this man a medal

hankconn commented 2 years ago

Hi, I've tried every combination of @sparticuz/chrome-aws-lambda package version, puppeteer-core package version, with or without the latest layer chrome-aws-lambda:31, dev dependency vs. prod dependency, etc., and yet I keep getting this error no matter what I try.

Example:

"@sparticuz/chrome-aws-lambda": "^14.3.0",
"puppeteer-core": "^14.3.0"

layer: arn:aws:lambda:us-west-1:764866452798:layer:chrome-aws-lambda:31

Is there a correct combination of package/layer versions that will get this working? or is it some other problem?

I'm doing this in serverless with typescript using esbuild

plugins:
  - serverless-esbuild

I've tried marking packages as "external" but it seems to make no difference - I just get this same error no matter what I try

custom:
  esbuild:
    bundle: true
    minify: false
    sourcemap: true
    external:
      - pg-native
      # - chrome-aws-lambda

Any ideas?

Error:

Error: Cannot find module '/var/task/src/cron/catelog/puppeteer/lib/Browser'
Require stack:
- /var/task/src/cron/catelog/testScript.js
- /var/runtime/UserFunction.js
- /var/runtime/Runtime.js
- /var/runtime/index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
    at Function.Module._load (internal/modules/cjs/loader.js:746:27)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:101:18)
    at Function.get puppeteer (/node_modules/@sparticuz/chrome-aws-lambda/source/index.ts:200:7)
    at Runtime.handler (/src/cron/catelog/testScript.ts:83:36)
    at processTicksAndRejections (internal/process/task_queues.js:95:5) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/task/src/cron/catelog/testScript.js',
    '/var/runtime/UserFunction.js',
    '/var/runtime/Runtime.js',
    '/var/runtime/index.js'
  ]
}
hessenh commented 2 years ago

@hankconn I had the same issue as you, also using esbuild. Manage to resolve this with the following config:

Serverless config:

package: {
        individually: true,
        patterns: ['!@sparticuz/**']
    },
    custom: {
        esbuild: {
            bundle: true,
            minify: false,
            sourcemap: true,
            define: {'require.resolve': undefined},
            platform: 'node',
            concurrency: 10,
            exclude: [
                "puppeteer-core", '@sparticuz/chrome-aws-lambda', "aws-sdk"
            ]
        },
    },

Package.json:

  "dependencies": {
    "@sparticuz/chrome-aws-lambda": "^16.0.0",
    "puppeteer-core": "^16.1.0"
  }

Usage:

const chromium = require("@sparticuz/chrome-aws-lambda");

Hope this works for you as well.

NanoCode012 commented 1 year ago

EDIT: I managed to fix it by configuring esbuild following @hessenh to add to Externals!

"build": "esbuild index.ts --bundle --minify --sourcemap --platform=node --target=es2020 --outfile=dist/index.js --external:@sparticuz/chrome-aws-lambda --external:puppeteer-core --external:aws-sdk",
"@sparticuz/chrome-aws-lambda": "~16.1.0",
 "puppeteer-core": "16.1.0"

ARN v31


Hello, I am also getting this issue. I hope someone can advise me. I followed the official AWS cli doc for TS: https://docs.aws.amazon.com/lambda/latest/dg/typescript-package.html#aws-cli-ts and usage for the upstream package: https://github.com/Sparticuz/chrome-aws-lambda#usage

I have tried setting Node version between 14 and 16, with varying version of this layer v26-v31, but still get same error.

import chromium from "@sparticuz/chrome-aws-lambda";
export const handler = async (
  event: APIGatewayEvent,
  context: Context
): Promise<APIGatewayProxyResult> => {
 const browser = await chromium.puppeteer.launch({
      args: chromium.args,
      executablePath: await chromium.executablePath,
      headless: chromium.headless,
    });
// ...
}

Building it with:

"scripts": {
    "prebuild": "rm -rf dist",
    "build": "esbuild index.ts --bundle --minify --sourcemap --platform=node --target=es2020 --outfile=dist/index.js",
    "postbuild": "cd dist && 7z a -r index.zip index.js*",
    "update": "yarn build && aws lambda update-function-code --function-name functionName --zip-file fileb://dist/index.zip"
  },
"devDependencies": {
    "@types/aws-lambda": "^8.10.102",
    "@types/node": "^18.7.14",
    "esbuild": "^0.15.7",
    "puppeteer-core": "^17.1.0"
  },
  "dependencies": {
    "@sparticuz/chrome-aws-lambda": "14.3.0"
  }

which gives me the below error

START RequestId: e559e8a8-b3b7-4128-b7fb-1d534035ed28 Version: $LATEST
2022-09-05T07:44:24.798Z    e559e8a8-b3b7-4128-b7fb-1d534035ed28    ERROR   Error: Cannot find module '/var/task/puppeteer/lib/Browser'
Require stack:
- /var/task/index.js
- /var/runtime/UserFunction.js
- /var/runtime/Runtime.js
- /var/runtime/index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
    at Function.Module._load (internal/modules/cjs/loader.js:746:27)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:101:18)
    at Function.get puppeteer [as puppeteer] (/var/task/index.js:49:11144)
    at Runtime.zC [as handler] (/var/task/index.js:49:11611)
    at Runtime.handleOnceNonStreaming (/var/runtime/Runtime.js:73:25) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/var/task/index.js',
    '/var/runtime/UserFunction.js',
    '/var/runtime/Runtime.js',
    '/var/runtime/index.js'
  ]
}
2022-09-05T07:44:24.855Z    e559e8a8-b3b7-4128-b7fb-1d534035ed28    ERROR   Invoke Error    {"errorType":"Error","errorMessage":"An error occured","stack":["Error: An error occured","    at Runtime.zC [as handler] (/var/task/index.js:49:12103)","    at Runtime.handleOnceNonStreaming (/var/runtime/Runtime.js:73:25)"]}
END RequestId: e559e8a8-b3b7-4128-b7fb-1d534035ed28
REPORT RequestId: e559e8a8-b3b7-4128-b7fb-1d534035ed28  Duration: 97.39 ms  Billed Duration: 98 ms  Memory Size: 128 MB Max Memory Used: 63 MB  Init Duration: 180.84 ms

I also tried the versions from the comment above mine, but it didn't help.

"puppeteer-core": "16.1.0"
"@sparticuz/chrome-aws-lambda": "~16.1.0"