npmscripts里的脚本怎么写

1.如何使用NPM作为构建工具npm Scripts Firstly, we need to figure out how npm can manage our build scripts. As part of npm's core, it has the npm run-script command ( npm run for short). This command dives into your package.json and pulls out the scripts Object. The first argument passed to npm run refers to a property in the scripts object - it will execute the property's value as a command in the operating systems default shell (usually Bash, except on Windows - but we'll get to that later). So let's say you have apackage.json config that looks like this:{"name": "myproject","devDependencies": {"jshint": "latest","browserify": "latest","mocha": "latest" },"scripts": {"lint": "jshint **.js","test": "mocha test/" } } If you run npm run lint - npm will spawn a shell and run jshint **.js . If you run npm run test , npm will spawn a shell and run mocha test/ . The shell environment has your node_modules/.bin folder added to the PATH which means any of the dependencies you have that install binaries will be runnable directly - in other words, no need to put "./node_modules/.bin/jshint **.js" or "$(npm bin)/jshint **.js" . If you run npm run without any arguments it gives you a list of the available commands to run, like so:Available scripts in the user-service package:lint jshint **.js test mocha test/ The npm run shell environment provides lots of helpful features to make sure your scripts are as succinct as they can be. For example the shell's PATH has your./node_modules/.bin/ folder inside of it, meaning any dependencies you install which have binaries can be called directly from a scripts shell. Also, there's a whole slew of super convenient environment variables that npm exposes, such as the currently running task, the package name and version, npm loglevel, and so on. Find them all out by making a script that runs env , and running it, like so:"scripts": {"env": "env" } Shortcut scripts npm also provides a few convinient shortcuts. The npm test , npm start , npm stop commands are all shortcuts for their run equivalents, e.g. npm test is just a shortcut for npm run test . These shortcuts are useful for 2 reasons:These are common tasks that most projects will use, and so it's nice to not have to type as much each time.Much more importanlty - it provides a standard interface within npm for testing, starting and stopping a package. Many CI tools, such as Travis, take advantage of this behaviour by making the default command for a Node.js project npm test . It also makes introducing new developers to your project a breeze, if they know they can simply run scripts like npm test without ever having to read any docs.Pre and Post Hooks Another cool feature about npm is that any script that can be executed also has a set of pre- and post- hooks, which are simply definable in the scripts object. For example, if you execute npm run lint , despite npm having no preconceived idea of what the lint task is, it will immediately run npm run prelint , followed by npm run lint , followed by npm run postlint . The same is true for any command, including npm test ( npm run pretest , npm run test , npm run posttest ). The pre and post scripts are also exit-code-sensitive, meaning if yourpretest script exits with a non-zero exit code, then NPM will immediately stop, and not run the test and posttest scripts. You can't pre- a pre- script though, soprepretest gets ignored. npm also runs the pre- and post- hooks for a few internal commands: install , uninstall , publish , update . You can't override the behaviours for the internal commands - but you can affect their behaviour with pre- and post- scripts. This means you can do cool stuff like:"scripts": {"lint": "jshint **.js","build": "browserify index.js > myproject.min.js","test": "mocha test/","prepublish": "npm run build # also runs npm run prebuild","prebuild": "npm run test # also runs npm run pretest","pretest": "npm run lint" } Passing Arguments Another cool feature with npm (since npm 2.0.0, at least ) is passing argument sets through to the underlying tools. This can be a little complex, but here's an example:"scripts": {"test": "mocha test/","test:xunit": "npm run test -- --reporter xunit" } With this config, we can simply run npm run test - which runs mocha test/ , but we can extend it with custom parameters with a -- prefix. For example npm run test -- anothertest.js will run mocha test/ anothertest.js , or more usefully npm run test -- --grep parser will expand to mocha test/ --grep parser (which runs only the tests with "parser" in the title). In the package.json we have test:xunit which effectively runs mocha test --reporter xunit . This setup can be。
2.怎么使用这个脚本首先你要在什么系统上挂 。.