Home

Run shell scripts using npm script

Do you ever wrote a shell script in your nodejs project. Shell scripts are very useful. Similarly npm scripts are also very useful to run processes either parallelly or sequentially. But do you know how to combine them and have more advantage. Here in this post I will show you how you can run shell script using npm script.

You can run your shell script through a npm script in your package.json similarly as you run them directly in the terminal. But there are some exceptions for this procedure too.

First let's see the simple method:

Let's say your shell script file is named build.sh

// ...
"scripts": {
   "build": "./build.sh"
},
// ...

For this method to work,make sure you have

chmod +x ./build.sh

Finally, the command to run build in npm would be

npm run build

You can either use absolute or relative path of your shell script

For Windows

In windows you can run .cmd using the above method.

"build": "build.cmd"

Using Git Bash for .sh

On Windows, you can use Git Bash to run shell script(.sh)

./build.sh

But you will get an error, if you do the same with npm-script

> npm run build
'.' is not recognized as an internal or external command, operable program or batch file.

Using Git Bash and npm-script for .sh

If you use Git Bash, you need to tweak the npm-script little bit to make it work.

// ...
"scripts": {
   "build": "bash ./build.sh"
},
// ...

Using cygwin and npm-script for .sh

If you use any cygwin terminal, you can even use the following way.

// ...
"scripts": {
   "build": "sh ./build.sh"
},
// ...


Last Updated on

Next Post: GraphProtocol: TS2322 null assignment in Subgraph →

Comments