How to set GitBash as the default NPM script runner on Windows

The issue

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?

The “but”

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

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

That’s all folks.

--

--

--

Senior Software Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Argo CD — No ugly deployment pipelines anymore!

Checklist for Creating Manual Builds for iOS Apps.

Linux kernel turns 30: congratulations from PVS-Studio

Under the Hood of PVS-Studio for Java: How We Develop Diagnostics

싱트해요! API 캠페인 03 AIG

S3 Bucket with Terraform + Uploading Files with ActiveStorage

Blue/Green Gitlab Runner CI Migration

Convert a CSV File to XLSX in C/C++

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Filipe Macêdo

Filipe Macêdo

Senior Software Engineer

More from Medium

“L&O” “d’oh, d’oh!

Exploring Express vs. Sinatra!

Is Diversity Pay Gap Reporting about creating a league table of offenders?

CS 373 Spring 2022: Saran Chockan Blog # 3