How to optimize disk space Node.js Modules Across Multiple Projects
Today I’ll guide you through a method to optimize disk space with node projects. I got a problem with disk space when working on a lot of NodeJS, and ReactJS repositories ( more than 30 lambdas repos node js, 3 repo ReactJS with ) for each project the node_modules
size up to 500MB ) ~ 33 * 500MB ~ 16.5 GB (ಥ﹏ಥ). I tried to review again the repositories and I realized that all repos are using a common library (lodash, webpack, jest, mocha….). I found a lot of different ways on the network and StackOverflow however I got a big deal with pnpn
the size of the package optimized up to 70%.
About the pnpm Fast, disk space efficient package manager:
- Fast. Up to 2x faster than the alternatives (see benchmark).
- Efficient. Files inside
node_modules
are linked from a single content-addressable storage. - Great for monorepos.
- Strict. A package can access only dependencies that are specified in its
package.json
. - Deterministic. Has a lockfile called
pnpm-lock.yaml
. - Works as a Node.js version manager. See pnpm env use.
- Works everywhere. Supports Windows, Linux, and macOS.
- Battle-tested. Used in production by teams of all sizes since 2016.
- See the full feature comparison with npm and Yarn.
Step 1: Install pnpm package (using npm)
Refer to the installation: https://pnpm.io/installation
(base) haithai@FVFY201CHV2H SafetyCheckManagementUI % npm install -g pnpm
/Users/haithai/.nvm/versions/node/v14.9.0/bin/pnpm -> /Users/haithai/.nvm/versions/node/v14.9.0/lib/node_modules/pnpm/bin/pnpm.cjs
/Users/haithai/.nvm/versions/node/v14.9.0/bin/pnpx -> /Users/haithai/.nvm/versions/node/v14.9.0/lib/node_modules/pnpm/bin/pnpx.cjs
+ pnpm@7.17.0
added 1 package in 1.752s
Step 2: Install the dependencies & devDependecies
The scenario is Project A vs Project B we have a lot of the same packages
Project A
Packages are hard linked from the content-addressable store to the virtual store.
Content-addressable store is at: /Users/haithai/Library/pnpm/store/v3
Virtual store is at: node_modules/.pnpm
Progress: resolved 1766, reused 0, downloaded 1766, added 1868, done
For project A — this is the first repo we run the install with pnpm so it doesn’t reuse any existing package from the node_modules root
So we have 1766 packages added with 0 resued package
Project B
Packages are hard linked from the content-addressable store to the virtual store.
Content-addressable store is at: /Users/haithai/Library/pnpm/store/v3
Virtual store is at: node_modules/.pnpm
Progress: resolved 2120, reused 1746, downloaded 374, added 2225, done