Running bash scripts with npm

node.jsBashShellNpm

node.js Problem Overview


I want to try using npm to run my various build tasks for a web application. I know I can do this by adding a scripts field to my package.json like so:

"scripts": {
   "build": "some build command"
},

This gets unwieldy when you have more complex commands with a bunch of options. Is it possible to move these commands to a bash script or something along those lines? Something like:

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

where npm run build would execute the commands in the build.sh file?

Reading through this post it seems like it is, but I'm not clear on exactly where I'm supposed to drop my build.sh file or if I'm missing something.

node.js Solutions


Solution 1 - node.js

Its totally possible...

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

also, make sure you put a hash bang at the top of your bash file #!/usr/bin/env bash

also make sure you have permissions to execute the file

chmod +x ./build.sh

Finally, the command to run build in npm would be

npm run build

Solution 2 - node.js

If you don't want to bother with giving permissions and the env you execute the script has for example sh, you could just do

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

Solution 3 - node.js

Even Simpler:

I routinely do this for one-offs and PoC's not involving a VCS

package.json
{
    "scripts": {
        "ship": "rsync -avz deployable/* <some-server>:/var/www/some-site/sub-dir/"
    },
}
...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark McKelvyView Question on Stackoverflow
Solution 1 - node.jseblahmView Answer on Stackoverflow
Solution 2 - node.jsrockadView Answer on Stackoverflow
Solution 3 - node.jsCNSKnightView Answer on Stackoverflow