Sometimes node_modules become massive that it makes hard and longer to deploy codes to the production environment. In this blog, I want to cover a few things that can be done to reduce node_modules size.
npm commands
- When deploying codes to production,
devDependencies
should be excluded as dependencies under that property literally for development purposes Eg. jest, typescript references etc.
Run npm ci --production
command to exclude devDependencies
from node_modules
- Sometimes after
npm install
, npm list shows the dependency tree which isn't necessarily the same as thenode_modules
file tree. However, this can be simplified by running
Run npm dedupe
Unify sub dependencies version
Sometimes developer faces a situation where multiple dependencies require sub-dependency but npm install multiple as they use different versions from each other. Eg axios, aws-sdk.
- In case, those dependencies are client libraries from your side, then you can simply update
package.json
in client libraries to use the same dependency version Eg. Below will install 2aws-sdk
packages into node_modules
library-a
|-----> aws-sdk@2.1.0
library-b
|-----> aws-sdk@2.2.0
Below will install 1 aws-sdk
package as there's only one version
library-a
|-----> aws-sdk@2.2.0
library-b
|-----> aws-sdk@2.2.0
- Usage of
overrides
property frompackage.json
can also resolve multiple versions issue. Eg.
{
...,
"dependencies": {
"library-a": "1.0.0",
"library-b": "1.0.5",
...
},
"overrides": {
"library-a": {
"aws-sdk: "2.2.0"
},
"library-b": {
"aws-sdk: "2.2.0"
}
}
}
Remove unnecessary packages
Sometimes developers tend to solve problems with npm packages and sometimes it's not good practice as it will pile up the size of node_modules quickly.
Common examples are lodash
, ramda
, and etc.
Try to avoid using these kind of libraries. If you can create it relatively quickly then just do it. Don't be lazy
Use cases
- The most useful use case is AWS lambda. AWS lambda has a size limitation (250mb unzip size) thus, having too many packages or duplicated dependencies may be a big cause of the issue.
Top comments (0)