How to set GitBash as the default NPM script runner on Windows
As a Windows user, you may face some issues trying to run NPM scripts that contain shell syntax.
Even if you try to use PowerShell, you may face syntax errors.
Ok, we know the CMD is legacy and it’s not able to run any code written in shell-script, but how about Powershell?
Isn’t Powershell able to run shell-script properly? The answer is YES. But, there are some tricky details about how NPM runs its scripts.
Internally NPM runs your scripts on the default command terminal, independently from where you are triggering the scripts.
This fact could confuse you a lot. You may think once you are running the script from PowerShell you will be able to run shell-script syntax. 💥
On Windows, it means it runs every script on CMD, and as I said before it’s not compatible with shell-script syntax.
The solution for it is pretty simple, you just need to set a shell-script syntax compatible terminal as the default NPM script runner.
I know I am talking about Powershell since the beginning, but I will personally recommend you to use GitBash, so check below how to set it as your default NPM script runner.
Open your cmd and run:
npm config set script-shell "C:\\Program Files (x86)\\git\\bin\\bash.exe"
For x64 installations
npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"
With this solution, I am considering you have GitBash installed at these locations. Always check before if it’s the location your GitBash is installed. Otherwise, you must change the path used on these examples by your current GitBash directory path.
Need to revert?
If you need to revert the configuration presented before, don’t worry. Run the following command to accomplish it.
npm config delete script-shell