Last active
October 28, 2021 19:24
-
-
Save drewwiens/9f5eacf2f85107916c47f311e53ce56b to your computer and use it in GitHub Desktop.
Simple shell script to prune unneeded packages from node_modules. For example, as a pre-processing step when building a Docker image of a Node app in an Nx monorepo that also contains frontend app(s). This reduced our Docker image by ~650 MB.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script is somewhat unsafe, but may be useful. This script deletes dependencies | |
# in package.json by deleting any lines that match | |
# the regexp in the grep, runs yarn install --prod, and then reverts package.json and | |
# yarn.lock back to their original contents. | |
# | |
# This script assumes yarn 1.x, i.e. yarn classic. If using npm instead of yarn 1.x: | |
# 1. Replace "yarn.lock" with "package-lock.json" | |
# 2. Replace "yarn install --prod" with "npm install --only=prod" | |
# | |
# The packages that this script is configured to prune are mostly Angular things. | |
# You can change what packages are pruned by editing the regexp in the grep. | |
echo 'Removing some packages that are not needed by Node apps in production...' | |
cp package.json package.json.bak | |
cp yarn.lock yarn.lock.bak | |
grep -Eiv 'angular[^\/]|@angular/animations|@angular/cdk|@angular/cdk-experimental|@angular/compiler|@angular/forms|@angular/material|@angular/platform-browser|@angular/platform-browser-dynamic|@angular/router|ngrx|ag-grid|browser|typeface' <package.json.bak >package.json | |
yarn install --prod || echo 'Ignoring erroneous postinstall error' | |
echo 'New size of node_modules:' | |
du -sh * | grep node_modules | |
rm package.json && rm yarn.lock | |
mv package.json.bak package.json && mv yarn.lock.bak yarn.lock | |
echo 'Done.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment