How to run a bash script with NPM
April 06, 2020
TLDR
Simply point to the bash script with a ./ at the front of the file path:
1."scripts": {2. "npmScriptName": "./bashScript.sh"3.}
Example for running a bash script with NPM
Make a simple bash script and include #!/usr/bin/env bash at the top:
1.#!/usr/bin/env bash2.printf 'test'
Then just provide a path to the file itself with a ./:
1."scripts": {2. "yup": "./test.sh"3.}
Then run the script:
1.npm run yup
It should output "test" in the terminal:
1.❯ npm run yup2.3.> Codinhood@0.1.0 yup /Users/cody/projects/codinhood4.> ./test.sh5.6.test
Problems
Check your permissions
If you do not have permissions to run the file you'll get an error
1.sh: ./test.sh: Permission denied2.npm ERR! code ELIFECYCLE3.npm ERR! errno 126
You can grant permissions by running chmod +x on unix.
1.chmod +x ./test.sh
Then running the commmand will work
1.❯ npm run yup2.3.> Codinhood@0.1.0 yup /Users/cody/projects/codinhood4.> ./test.sh5.6.test
Forgot ./
If you don't put . before the /, like /test.sh, then you will get the following error:
1.❯ npm run yup2.3.> Codinhood@0.1.0 yup /Users/cody/projects/codinhood4.> /test.sh5.6.sh: /test.sh: No such file or directory7.npm ERR! code ELIFECYCLE8.npm ERR! syscall spawn
Because it's looking for a directory instead of an executable. If you don't have either . or / then it will think test.sh is a command:
1.❯ npm run yup2.3.> Codinhood@0.1.0 yup /Users/cody/projects/codinhood4.> test.sh5.6.sh: test.sh: command not found7.npm ERR! code ELIFECYCLE8.npm ERR! syscall spawn9.npm ERR! file sh10.npm ERR! errno ENOENT
More NPM Snippets
Popular Articles
I Can't Believe It's Not CSS: Styling Websites with SQL
Style websites using SQL instead of CSS. Database migrations for your styles. Because CSS is the wrong kind of declarative.

How I Built an Oreo Generator with 1.1 Sextillion Combinations
Building a web app that generates 1,140,145,285,551,550,231,122 possible Oreo flavor combinations using NestJS and TypeScript.

AI Model Names Are The Worst (tier list)
A comprehensive ranking of every major AI model name, from the elegant to the unhinged. Because apparently naming things is the hardest problem in AI.