Reducing Bundle Size

Serverless environments are sensitive to bundle size, since it has to download and unzip the contents. To help reduce cold start times as much as possible, you should remove any unnecessary files in the node_modules or report offending dev libraries in this PR (opens in a new tab)

ℹ️

Next 14+ no longer includes dev dependencies like @swc, esbuild, etc... in the output node_modules, so please upgrade at your earliest convenience.

Unzipped size must be smaller than 262144000 bytes

To identify the module that's taking up too much space (and isn't serverless friendly):

du -hs .open-next/server-function/node_modules/* | sort -rh

If your app requires the offending library, then consider moving your business logic of the api to its own lambda, eg: /api/v2 => Api Lambda

ℹ️

There is a PR (opens in a new tab) to remove some dev dependency from the output node_modules but that requires more testing before it can merge.

Common issues

Sharp

sharp is not needed outside of the Image Optimization function so you should not have it as a dependency. But if you are depending on sharp, be sure to install it with the correct flags for your lambda. eg: --arch=arm64 --platform=linux --target=18 --libc=glibc

pdfjs
  • If you need to use pdfjs, you should install it with npm i pdfjs-dist--no-optional because the optional dep: canvas takes about 180MB.

  • If the above doesn't work (or gives some compilation errors) you can try:

  experimental: {
    outputFileTracingExcludes: {
      "*": ["node_modules/canvas"],
    },
  },
Others

Please open an issue or let us know on discord if there are any other modules that are causing issues.