'How to reduce the size of my node js lambda function?

My Node lambda is a fairly small function in terms of what it does/lines of code. yet somehow is sitting at 18mb in size. I think this might be due to me installing firebase and it being quite a big package.

I was wondering if there was a way to reduce it?

I did have const firebase = require('firebase') but now I changed it to:

const firebase = require('firebase/app')

but when I zip the lambda up I can still see lines this like:

 adding: node_modules/firebase/messaging/dist/ (stored 0%)
 adding: node_modules/firebase/messaging/dist/index.cjs.js.map (deflated 20%)
 adding: node_modules/firebase/messaging/dist/index.cjs.js (deflated 3%)

which looks like it's still adding the messaging package for some reason?

is there a way to reduce this lambda as I think 18mb is pretty big



Solution 1:[1]

You may use lambda layers to solve this problem. We had to use facebook node.js sdk in our lambda and it was huge(~7MB zipped version), we used lambda layers to reduce deployment package.

Layers let you keep your deployment package small, which makes development easier. You can avoid errors that can occur when you install and package dependencies with your function code. For Node.js, Python, and Ruby functions, you can develop your function code in the Lambda console as long as you keep your deployment package under 3 MB.

Please check this link for more information

Solution 2:[2]

This is one of drawbacks of Node.js, which was addressed as an improvement in Deno, as Node.js forces you to compile to whole package even if you are using only one function of it!

There's two ways to get around this:

  1. Load the package remotely in runtime, using a package like require_from_url for example, or doing it yourself with a fetch call, then eval Be careful with that though!

  2. After setting up the environment, and before executing, run a script that finds the unused packages, -depcheck might help you with that, and remove them! Simply many packages install dozens of dependencies just in case they needed them, so removing them might reduce your app's size.

Bare in mind that both solutions come with their overhead, that's why unless size is crucial to you, I would suggest to just live with it rather than taking to risk.

Solution 3:[3]

One quick win will be adding a .yarnclean file to delete junk from node_modules.

I see you have .js.map files being included in the final artifact which doesn't make sense. Those files are useless in runtime.

You can grab my version of .yarnclean file here and check out other "quick wins" in my article here

Solution 4:[4]

As I understand the best approach is to use a bundler like webpack with a code tree shaking. It removes all unused code from your bundle.

In case if you use webpack, enable the following in the webpack.config.js:

optimization: {
    minimize: true,
    minimizer: [new TerserPlugin()],
},

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ersoy
Solution 2 Ahmed Hammad
Solution 3 Vlad Holubiev
Solution 4