Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save raduserbanescu/410d796f7a311b0f4f7d7214d5eaeebe to your computer and use it in GitHub Desktop.
Save raduserbanescu/410d796f7a311b0f4f7d7214d5eaeebe to your computer and use it in GitHub Desktop.
On Windows, run npm scripts through Bash instead of cmd.exe

Run npm scripts through Bash instead of cmd.exe

On Windows, npm scripts run under cmd.exe.

If you have Git for Windows installed, you can make npm scripts run under Bash. This allows you to somewhat mimic a Linux or macOS environment.

Check to see where npm is installed:

npm ls -g --depth=0

If you are using the npm that comes with Node.js, edit the file:
C:\Program Files\nodejs\node_modules\npm\lib\utils\lifecycle.js

If you have updated npm, edit the file:
C:\Users\__USERNAME__\AppData\Roaming\npm\node_modules\npm\lib\utils\lifecycle.js

   if (!unsafe) {
     conf.uid = uid ^ 0
     conf.gid = gid ^ 0
   }

   var sh = 'sh'
   var shFlag = '-c'

   if (process.platform === 'win32') {
-    sh = process.env.comspec || 'cmd'
-    shFlag = '/d /s /c'
-    conf.windowsVerbatimArguments = true
+    sh = 'C:\\Program Files\\Git\\usr\\bin\\bash.exe'
   }

   log.verbose('lifecycle', logid(pkg, stage), 'PATH:', env[PATH])
   log.verbose('lifecycle', logid(pkg, stage), 'CWD:', wd)
   log.silly('lifecycle', logid(pkg, stage), 'Args:', [shFlag, cmd])

   var proc = spawn(sh, [shFlag, cmd], conf)

   proc.on('error', procError)
   proc.on('close', function (code, signal) {

Note: After an update the file will be overwritten, so you will need to apply the changes again.

Tested on: npm 4.6.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment