简体   繁体   中英

How to generate PDF by chromium in AWS lambda, node.js v18

I am trying to generate PDF by @sparticuz/chromium using AWS lambda, node.js v18, layer and architecture: x86_64.

I get the error:

Runtime.ImportModuleError: Error: Cannot find module '@sparticuz/chromium'

Layer:

{
  "name": "chrome_layer",
  "dependencies": {
    "@sparticuz/chromium": "108.0.1",
    "puppeteer-core": "18.0.5"
  }
}

Code:

const chromium = require('@sparticuz/chromium')
const puppeteer = require('puppeteer-core')

const browser = await puppeteer.launch({
      args: chromium.args,
      executablePath: await chromium.executablePath,
    })

I had a similar problem. In my case I was trying to deploy the lambda with AWS CDK, without a separate layer.

When you create the NodejsFunction, inside NodejsFunctionProps, pass 2 things:

  1. the package-lock.json file:

    depsLockFilePath: 'src/your-lambda-path/package-lock.json',

  2. make sure the @sparticuz/chromium node module won't be bundled as a single file, but rather passed as is in node_modules folder

    bundling.nodeModules: [ '@sparticuz/chromium', ],

This way the binary for chromium will be installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM