Skip to content

Instantly share code, notes, and snippets.

@classmember
Last active September 6, 2017 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save classmember/0412182608b2dc4588e8790a2add97ee to your computer and use it in GitHub Desktop.
Save classmember/0412182608b2dc4588e8790a2add97ee to your computer and use it in GitHub Desktop.
npm test "Error: Cannot find module" solution - this shell script installs all missing npm packages
#!/bin/sh
# npm test wouldn't run due to missing or unresolved packages.
# This script installs all missing packages from error messages.
# Fix missing packages
missing_package="$(
npm test 2>&1 |
grep 'Cannot find module' |
cut -d\' -f2)"
while [ -n "${missing_package}" ] ; do # while missing packages
echo "[+] installing ${missing_package}" # report what is installing
npm install "${missing_package}" # install the package
missing_package="$( # update missing_package for loop
npm test 2>&1 |
grep 'Cannot find module' |
cut -d\' -f2)"
done
# Fix unresolved packages
missing_package="$(
npm test 2>&1 |
grep "Can't resolve" |
cut -d\' -f3)"
while [ -n "${missing_package}" ] ; do # while missing packages
echo "[+] installing ${missing_package}" # report what is installing
npm install "${missing_package}" # install the package
missing_package="$( # update missing_package for loop
npm test 2>&1 |
grep "Can't resolve" |
cut -d\' -f3)"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment