Skip to content

Instantly share code, notes, and snippets.

@jondlm
Created July 27, 2022 21:47
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 jondlm/f2b40b7f27dabdca8994980bf4dbe025 to your computer and use it in GitHub Desktop.
Save jondlm/f2b40b7f27dabdca8994980bf4dbe025 to your computer and use it in GitHub Desktop.
little script to debug node module resolution
#!/usr/bin/env bash
if [ -z "$1" ]; then
echo 'first arg should be module to search up for'
exit 1
fi
BLUE=$(tput setaf 4)
GREEN=$(tput setaf 2)
RED=$(tput setaf 1)
NORMAL=$(tput sgr0)
blue () {
printf "${BLUE}%-12s${NORMAL}%s\n" "$1" "$2"
}
red () {
printf "${RED}%-12s${NORMAL}%s\n" "$1" "$2"
}
green () {
printf "${GREEN}%-12s${NORMAL}%s\n" "$1" "$2"
}
# Ensure we're matching node's realpath behavior
blue "realpath" "switching to the real path of current dir:"
blue "realpath" " $PWD"
blue "realpath" " ↓"
blue "realpath" " $(realpath .)"
cd $(realpath .)
blue "info" "performing a 'find-up' for 'node_modules'"
while [ "$PWD" != "/" ]; do
D="node_modules/$1"
ABS_D="$PWD/$D"
blue "info" "checking $ABS_D"
if [[ -d "$D" || -L "$D" ]]; then
green "success" "module $1 found:"
green "realpath" " $ABS_D"
green "realpath" " ↓"
green "realpath" " $(realpath "$ABS_D")"
green "version" " $(jq -r .version < "${ABS_D}/package.json")"
exit 0
fi
blue "info" "going up a directory"
cd ..
done
blue "info" "stopped because we reached /"
red "fail" "module not found"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment