Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how do you install external dependencies? #118

Open
JaredAAT opened this issue Apr 30, 2021 · 4 comments
Open

how do you install external dependencies? #118

JaredAAT opened this issue Apr 30, 2021 · 4 comments

Comments

@JaredAAT
Copy link

JaredAAT commented Apr 30, 2021

when looking at this part of the documentation:

external Array of modules to be copied into node_modules instead of being loaded into browserify bundle. Note that external modules will require it's dependencies within it's directory. (cd external_modules/some-module && npm i --prod)

given a serverless.yml of:

functions:
  resultpdf:
      handler: handler.resultPDF
      runtime: nodejs12.x
      timeout: 20
      memorySize: 1600
      optimize:
        external: ["chrome-aws-lambda"]

how do you go about running cd external_modules/chrome-aws-lambda && npm i --prod?

@alecmcgovern
Copy link

@JaredAAT did you ever figure this out? I'm working with the same package chrome-aws-lambda and struggling to include all its dependencies.

@alecmcgovern
Copy link

If anyone else runs into this, the solution is to cd into your node_modules in the root of your project and run the install command. This way all the dependencies are included when the external packages are copied over to the optimized build. I added a postinstall command to my package.json so I didn't have to think about running this anytime the repo was cloned or my packages were re-installed.

Note: I also needed to include puppeteer-core in the external packages, and do a similar production install for that package.

serverless.yml

  functionName:
    handler: functions/index.handler
    optimize:
      external: ['chrome-aws-lambda', 'puppeteer-core']

package.json

  "scripts": {
    ...
    "postinstall": "yarn run install:chrome-aws-lambda && yarn run install:puppeteer",
    "install:chrome-aws-lambda": "cd node_modules/chrome-aws-lambda && npm i --production",
    "install:puppeteer": "cd node_modules/puppeteer-core && npm i --production",
  },

@wcpines
Copy link

wcpines commented Jul 22, 2022

Hey @alecmcgovern I've attempted this solution and when I run it locally (npm exec serverless offline start) I get

Warning: Invalid configuration encountered
  at 'functions.GeneratePdf': unrecognized property 'optimize'

Learn more about configuration validation here: http://slss.io/configuration-validation

I'm not certain the solution you're suggesting is the answer to my own issue, but it seems related. I'm getting the following error when I actually run the lambda:


Error: Error: Could not find expected browser (chrome) locally. Run `npm install` to download the correct Chromium revision (901912).

Relevant part of the code in question:

import chromeAwsLambda from 'chrome-aws-lambda';
import { PDFOptions } from 'puppeteer-core';

export class PDFGenerator {
  static genPdf = async (url: string, options: PDFOptions ): Promise<Buffer> => {
    console.log(`Offline: ${process.env.IS_OFFLINE}`)
    const executablePath = process.env.IS_OFFLINE == 'true'
      ? './node_modules/puppeteer/.local-chromium/mac-1011831/chrome-mac/Chromium.app/Contents/MacOS/Chromium'
      : await chromeAwsLambda.executablePath;

    console.log(`Exec Path: ${executablePath}`)
    const browser = await chromeAwsLambda.puppeteer.launch({
      args: chromeAwsLambda.args,
      executablePath
    });
    const page = await browser.newPage();

Using node16 fwiw.

@JaredAAT
Copy link
Author

@wcpines The first error you're seeing is probably because you're using serverless 2/3 and this package isn't actively maintained anymore: #133 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants