# 3x Smaller Lambda Artifacts by Removing Junk From node_modules

Cut AWS Lambda node_modules size by 3x without webpack using practical cleanup steps.

- Published: 2020-11-02
- Reading time: 4 min read
- Author: Vladyslav Holubiev
- Medium: https://medium.com/shelf-io-engineering/3x-smaller-lambda-artifacts-by-removing-junk-from-node-modules-2b50780ca1f5

Reducing artifact size proportionally correlates with the cold start latency.

The most obvious way would be to use webpack and do tree shaking. But I'm not a fan of that approach. It's cumbersome to set up and has significant dev effort, especially when you face issues with bundling some backend dependencies.

I'll talk about some "quick wins" instead.

![AWS Lambda cold start latency chart showing how smaller deployment packages reduce cold starts](https://vladholubiev.com/_next/static/immutable/media/image-01.2v7d_h6uue47d.webp)

> We'll use a tool called [fdu](https://github.com/amio/flaming-disk-usage) to inspect junk in `node_modules`.

We'll take one of my repos as an example. When installing only production dependencies, the size of `node_modules` folder is 149.62 MB (36.1 MB zipped). At the end of the article, we'll reduce it down to 62.12 MB (12.2 MB zipped).

![Flaming Disk Usage treemap of the initial 149.62 MB node_modules directory before cleanup](https://vladholubiev.com/_next/static/immutable/media/image-02.0qjkpuhy3y9vo.webp)

## Don't bundle aws-sdk

This is the most obvious suggestion but still brings the most value. It helped me to reduce my zip artifact size **by 7 MB**.

![Flaming Disk Usage treemap highlighting the aws-sdk package size inside node_modules](https://vladholubiev.com/_next/static/immutable/media/image-03.2yq6v42_h96m0.webp)

In case you don't need the latest version of `aws-sdk` — just drop it from your artifact size.

Not adding it to `package.json` is not an option, because your tests might depend on it, and additionally, other dependencies might still install `aws-sdk` without you knowing it.

We'll combat this problem in three steps.

## Track currently available aws-sdk version in Lambda runtime

AWS maintains a page with the current aws-sdk version here: [https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html](https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html)

I've created an automatic alert to notify me when AWS puts a new `aws-sdk` version inside Lambda runtime. I like the [Distill Chrome Extension](https://distill.io/), it's free.

Now we know for sure on which `aws-sdk` version we can rely upon in our code.

## Use [yarn resolutions](https://classic.yarnpkg.com/en/docs/selective-version-resolutions/) to ensure only 1 version of aws-sdk is installed

This is a wonderful feature if you're using yarn. Add these lines to your `package.json` and yarn will install only 1 instance of `aws-sdk` dependency, regardless of what other packages require.

```json
"resolutions": {
  "aws-sdk": "2.712.0"
}
```

## Exclude aws-sdk from final artifact zip

This step might depend on the way you create a zip artifact, but it looks for me as:

```bash
zip --quiet -r --exclude="node_modules/aws-sdk/*" \
  artifact.zip \
  lib node_modules
```

## Exclude TypeScript typings

This step helped me to remove **16 MB** of junk (**2.2 MB** zipped) from the final artifact zip.

Whether you use TypeScript or not, many package ship typings to the NPM. They're useful during development but have no impact on the runtime.

Here is a command to remove them:

```bash
npx del-cli \
  "node_modules/**/@types/**" \
  "node_modules/**/*.d.ts" \
  "node_modules/**/.yarn-integrity" \
  "node_modules/**/.bin"
```

## Exclude tests & browser builds with .yarnclean

This step reduced node_modules size by **19.5 MB** (**14.6 MB** zipped).

Another great yarn feature — [.yarnclean](https://classic.yarnpkg.com/en/docs/cli/autoclean/) file. You define glob patterns and yarn removes them from `node_modules` when running `yarn install`.

What type of stuff is useful to remove?

- test files (

  *.test.js

  ) — surprisingly many npm packages publish their tests to NPM. And you keep deploying this junk to Lambda runtime.
- IDE configurations: 

  .idea/*

   , 

  .vscode/*

  ,

   

  .eslintrc

  , etc
- Graphic assets used for illustration purposes in

   

  README.md
- Browser builds: 

  *.min.js

  , 

  dist/*.js

   , etc

Just add a `.yarnclean` file to your repository with the list of glob patterns and enjoy size savings out of the box!

You can initialize a fresh config by running `yarn autoclean --init`, or use my own collection of junk patterns I collected over the past year below.

> I compiled my own `.yarnclean` file with the junk I found in my repos: [https://gist.github.com/vladholubiev/1c0e9fd4d569446cada21e4a3c64d0f8](https://gist.github.com/vladholubiev/1c0e9fd4d569446cada21e4a3c64d0f8)

## Before & After

![Before and after comparison showing node_modules shrinking from 149.62 MB to 62.12 MB after cleanup](https://vladholubiev.com/_next/static/immutable/media/image-04.0kucw04awdsr5.webp)

## Bonus: Add more dependencies to yarn resolutions

It might depend on the codebase, but my projects and the dependencies I use depend on `lodash` a lot. When running `yarn install` I end up with many different versions of `lodash` installed, which differ only by patch increment.

Here how it looks like in my `yarn.lock` file.

```text
lodash@4.17.15:
  version "4.17.15"
  ...

lodash@^4.17.11
  version "4.17.11"
  ...
lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19:
  version "4.17.20"
  ...
```

This is slightly risky, but I am ok with using 1 single lodash version in my repo to reduce Lambda size. Just update your `resolutions` section of the `package.json` file to look like:

```json
"resolutions": {
  "aws-sdk": "2.712.0",
  "lodash": "4.17.20"
}
```

After this change, I won another 15.5 MB of space!

![Disk usage comparison showing an additional 15.5 MB saved after deduplicating packages with yarn resolutions](https://vladholubiev.com/_next/static/immutable/media/image-05.1qdh_aulrj290.webp)

Keep lurking inside your `yarn.lock` file to find other inefficiencies.
