Skip to content

Instantly share code, notes, and snippets.

@KylePDavis
Last active June 9, 2020 18:04
Show Gist options
  • Save KylePDavis/1941af66fd3b13bcf36d59495729fedf to your computer and use it in GitHub Desktop.
Save KylePDavis/1941af66fd3b13bcf36d59495729fedf to your computer and use it in GitHub Desktop.
A simple way to symlink local npm packages into your node_modules for testing
#!/bin/bash -e
################################################################################
# Links a local Node.js package directory into the current node_modules/ folder.
# This is simpler and more isolated than `npm link` so it's better for quick one-off testing.
#
# USAGE:
# ~/bin/ln-pkg.sh LOCAL_PKG_DIR [LOCAL_PKG_DIR_2]
#
################################################################################
[ -d "node_modules" ] || { echo "missing node_modules/"; exit 1; }
for D in "$@"; do
if [ -d "$D/packages/" ]; then
"$0" "$D/packages/"*/
else
N=$(F="$D/package.json" node -p 'require(process.env.F).name')
L=$(echo "$N" | grep / | cut -f1 -d/ || echo "$N")
rm -fr "node_modules/$N"
mkdir -p "node_modules/$L"
ln -sv "$(cd "$D"; pwd)" "node_modules/$N"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment