Hi! I've seen more questions about puppeteer and l...
# help
d
Hi! I've seen more questions about puppeteer and layers but none of the answers worked for me, so I'm just going to give it a try here... I'm using
node v14.19.1
with
npm 8.9.0
. Following https://serverless-stack.com/examples/how-to-use-lambda-layers-in-your-serverless-app.html, it throws the error
Copy code
Cannot find module '/home/dev/layer-chrome-aws-lambda/node_modules/puppeteer-core/lib/cjs/puppeteer/common/Browser'.
This is, by the way, after fixing the
Cannot find a handler file for "lambda.handler"
error by moving
backend/functions/lambda.ts
to
backend/lambda.ts
(I think the tutorial outdated the new directory structure). Removing the
bundle: { externalModules: ["chrome-aws-lambda"] },
line results in another error:
Copy code
ReferenceError: __dirname is not defined in ES module scope This file is being treated as an ES module because it has a '.js' file extension and '/home/dev/layer-chrome-aws-lambda/.sst/artifacts/dev-layer-chrome-aws-lambda-MyStack-Api-Lambda_GET_-/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
I've tried multiple workarounds (including downgrading node and npm versions but that caused a lot of other issues) but none seem to work. Any thoughts? Below are the dependencies of my `package.json`:
Copy code
{
  "devDependencies": {
    "@serverless-stack/cli": "^1.0.4",
    "@serverless-stack/resources": "^1.0.4",
    "@tsconfig/node14": "^1.0.1",
    "typescript": "^4.6.4"
  },
  "workspaces": [
    "backend"
  ],
  "dependencies": {
    "chrome-aws-lambda": "^10.1.0",
    "puppeteer": "^14.0.0",
    "puppeteer-core": "^14.0.0"
  }
}
m
You need to install the dependencies in the backend folder
@Dimitri van Hees the example was not yet updated. run the below command in
backend/
folder and check if it works.
Copy code
npm install puppeteer puppeteer-core chrome-aws-lambda
d
ah it works! (after also saving the generated tsconfig ;-)). thank so much!
m
The examples will be updated today, thanks 🙂