This script automatically loads all npm
scripts from the package.json
in your current directory, allowing you to run them directly as commands without using npm run
.
- Automatic Detection: When you
cd
into a directory with apackage.json
, all scripts are instantly available as commands. - Dynamic Updates: Switching directories refreshes the commands, ensuring only relevant scripts are available.
- Simple Setup: Just source the script in your
~/.zshrc
and you're ready to go.
- Download the Script
curl -o ~/npm-scripts-auto.sh https://path-to-your-script.sh
(Replace the URL with the actual script location if hosting it somewhere)
Or create the file manually:
touch ~/npm-scripts-auto.sh
nano ~/npm-scripts-auto.sh
Then paste the script content into it.
- Make It Executable (Optional)
chmod +x ~/npm-scripts-auto.sh
- Source the Script in Zsh
Edit your ~/.zshrc
:
nano ~/.zshrc
Add this line:
source ~/npm-scripts-auto.sh
Reload your shell:
source ~/.zshrc
Just cd
into any project folder with a package.json
:
cd ~/projects/my-app
Assuming your package.json
looks like this:
{
"scripts": {
"start": "node app.js",
"build": "webpack",
"test": "jest"
}
}
You can now run:
start # Runs 'npm run start'
build # Runs 'npm run build'
test # Runs 'npm run test'
No need for npm run
!
- Uses the Zsh
chpwd
hook to detect when you change directories. - Parses the
package.json
withjq
to extract all script names. - Dynamically creates functions named after the scripts, which internally run
npm run <script>
.
- Zsh (this won't work with Bash or other shells).
- jq for parsing JSON:
# macOS
brew install jq
# Debian/Ubuntu
sudo apt-get install jq
If you want to disable this feature:
- Remove or comment out the line in your
~/.zshrc
:
# source ~/npm-scripts-auto.sh
- Reload the config:
source ~/.zshrc
MIT License. Feel free to modify and adapt as needed.