Skip to content

Instantly share code, notes, and snippets.

@zlocate
Last active August 15, 2020 15:25
Show Gist options
  • Save zlocate/661d532f37a62167cb07018fe8163051 to your computer and use it in GitHub Desktop.
Save zlocate/661d532f37a62167cb07018fe8163051 to your computer and use it in GitHub Desktop.
How to make npm much faster.
#!/bin/bash
# Backup current npm configs (local and global)
now="$(date +'%d.%m.%Y')"
echo "Backup npm config to npm.config[$now].json.bak file"
npm config list --json > "npm.config\[$now\].json.bak"
echo "Backup npm global config to npm.config-$now.global.json.bak file"
npm config list --global --json > "npm.config-$now.global.json.bak"
# Extract default NPM init username from git config
INIT_USERNAME=$(git config --global --get user.name)
# Extract default NPM init email from git config
INIT_EMAIL=$(git config --global --get user.email)
# Assosiative arrays works from bash 4.x.x
declare -A npmConfig=(
[prefer-offline]=true
[audit]=false
[init-author-name]=$INIT_USERNAME
[init-author-email]=$INIT_EMAIL
)
for key in "${!npmConfig[@]}"; do
value=${npmConfig[$key]};
echo "Sets npm options (global) $key to $value";
sudo npm config --global set "$key" "$value"
done

If you want to speed up dependencies installing of your node project try to use this npm options: Sometimes this dramatically increase installing times

Use this flags (or npm options):

--prefer-offline - prefer use a local version of packages instead check ones at registry.

--no-audit - disable security checking of used dependencies (may be insecure)

--no-progress - disable progress bar (thats increase performance of dependencies installing OMG)

--no-optional - ignore installing optional dependencies

--ignore-scripts - disable running post-install scripts that may be dangerous

You can use it as npm cli flags or sets this at .npmrc at your project dir or sets by npm config set

@zlocate
Copy link
Author

zlocate commented Aug 9, 2020

But if you are looking for a better solution - use local (cache) registry, e.g verdaccio (https://verdaccio.org/)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment