最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

As we know, you can run arbitrary commands using npm run by adding a scripts hash to your package.json:

"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 child_process.exec to run arbitrary commands from within a standard node script file:

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

But I've noticed child_process 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 package.json npm run scripts out into separate files? Preferably one that doesn't require bash?

As we know, you can run arbitrary commands using npm run by adding a scripts hash to your package.json:

"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 child_process.exec to run arbitrary commands from within a standard node script file:

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

But I've noticed child_process 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 package.json npm run scripts out into separate files? Preferably one that doesn't require bash?

Share Improve this question asked Apr 21, 2014 at 16:05 user846062user846062
Add a comment  | 

3 Answers 3

Reset to default 14

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 (slightly modified for clarity):

// scripts/favicon.js

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

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


// package.json

"scripts": {
  "build-favicon": "node scripts/favicon.js",
},
"devDependencies": {
  "favicons": "latest",
}

Which would be run in the terminal with the command npm run build-favicon

Using the make command is also a nice option.

Makefile

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

package.json

"scripts": {
    "build-js": "make build-js",
}

You can find more about make here.

Using node to run a JS file is an option, but if you need to run system commands on both Windows and *NIX, that would make the script messy. For cross-platform scripts, try out the shelljs package.

发布评论

评论列表(0)

  1. 暂无评论