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
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
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:
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!
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.
©2020 All rights reserved.