Running npm Scripts Containing Bash On Windows

You may at some point find yourself using a Windows machine and needing to run an npm script that uses Bash. Typically, this would cause instant errors. Fortunately, though, there is a simple solution to this dilemma.

Here’s an example of an npm script that will fail on Windows:

{
  "name": “something”,
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    “show-directory-contents”: “ls -a -t”
  },
  …

In order to run npm scripts like this, we need to do two simple things. We need to, first, install Git so we can utilize it’s standalone shell and then we need to configure the npm script shell location to use Git’s standalone Git Bash shell.

Install Git

To install Git for Windows, navigate to https://git-scm.com/download/win. Run git in Command Prompt afterwords to make sure Git was installed properly.

Configure the npm script shell location

After git is installed, open Command Prompt and run npm config set script-shell "%userprofile%\AppData\Local\Programs\Git\bin\bash.exe". This will force all npm scripts to run within the Git Bash standalone shell rather instead of using the default Windows configuration, allowing you to now run npm scripts containing Bash within a Windows environment.

Now, try running an npm script with Bash in it and see what happens!