J. Ky Marsh J. Ky Marsh - 1 year ago 144
Javascript Question

Externally-located 'npm run' scripts in package.json on Windows?

As we know, you can run arbitrary commands using

npm run
by adding a
hash to your

"scripts": {
"build-js": "browserify browser/main.js | uglifyjs -mc > static/bundle.js"

Which would then be run with
npm run build-js

You can also move these commands out into separate scripts, such as bash scripts, as such:

"scripts": {
"build-js": "bin/build.sh"

This obviously doesn't natively work on Windows, due to Windows not being capable of running bash scripts. You can install bash ports and such, but I'd love to be able to use some sort of native Windows construct for doing the same thing.

I've tried some other approaches, including using
to run arbitrary commands from within a standard node script file:

"scripts": {
"build-js": "node bin/build.js"

But I've noticed
chokes on relatively large/intensive operations, making it implausible to use.

Is there a Windows-specific (or even better, cross-platform) way to move these
npm run
scripts out into separate files? Preferably one that doesn't require bash?

Answer Source

From a helpful article on using NPM as a build tool, why not simply use a javascript file?

Here's the example given in the article:

// scripts/favicon.js

var favicons = require('favicons');  
var path = require('path');  

  source: path.resolve('../assets/images/logo.png'),
  dest: path.resolve('../dist/'),

// package.json

"devDependencies": {
  "favicons": "latest",
"scripts": {
  "build:favicon": "node scripts/favicon.js",
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download