netlifynetlify-function

How do I exclude Node modules from the Netlify function bundle?


I have a Netlify function that uses Puppeteer to render PDFs. Specifically, puppeteer-core and chrome-aws-lambda. Between these two modules, the bundle size is almost exactly at the 50MB limit for a function (this limitation is from AWS Lambda which Netlify Functions use). So much so that importing the S3 client from the ask-sdk (version 3) package tips the scale and the function can no longer be deployed.

Lambda's already have the aws-sdk included by default so bundling the module is unnecessary. However, because it's being imported, Netlify's build step thinks it should be included.

How can I exclude a module(s) from the function bundle?


Solution

  • In your netlify.toml there is an undocumented option for [functions] called ignored_node_modules that lets you specify an array of Node modules to exclude from the bundle.

    The following will ignore the modules across all functions.

    [functions]
      ignored_node_modules = ["aws-sdk"]
    

    Or you can target a specific function by name

    [functions.render]
      ignored_node_modules = ["aws-sdk"]
    

    Note that all Lambdas running Node (Even Node 16 LTS) are still using aws-sdk (version 2) and not @aws-sdk (version 3).

    For anyone curious, I discovered this option here in the Netlify CLI source.