简体   繁体   中英

How to use chrome-aws-lambda module with AWS lambda functions?

I am trying to generate pdf from HTML using chrome-aws-lambda by node.js lambda function. I created a Layer using this manual . Also, I tried to use existing layer with my function but I always get an error: Cannot read property 'puppeteer' of undefined . My handler looks like:

import chromium from 'chrome-aws-lambda';    
export const handler: Handler = async (event: AppsyncEvent) => {
      ...
      try {
        browser = await chromium.puppeteer.launch({
          args: chromium.args,
          defaultViewport: chromium.defaultViewport,
          executablePath: await chromium.executablePath,
          headless: chromium.headless
        });
    ....
      } finally {
    ....
      }
    }

webpack settings in serverless.yml

webpack:
    webpackConfig: ../webpack.config.js
    includeModules:
      packagePath: '../package.json'
      forceInclude:
        - source-map-support
      forceExclude:
        - aws-sdk
        - chrome-aws-lambda

webpack.config.js

module.exports = {
    mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
    devtool: 'source-map',
    resolve: {
        extensions: ['.js', '.jsx', '.json', '.ts', '.tsx']
    },
    output: {
        libraryTarget: 'commonjs',
        path: path.join(__dirname, '.webpack'),
        filename: '[name].js'
    },
    target: 'node',
    externals: ['chrome-aws-lambda'],
    module: {
        rules: [
            {test: /\.tsx?$/, loader: 'ts-loader'}
        ]
    },
    plugins: []
}

Could you help me to resolve this issue?

So, the problem was with imports. I changed

import chromium from 'chrome-aws-lambda'

to

const chromium = require('chrome-aws-lambda')

the error is gone

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