05 npm scripts
简介
package.json 中的 scripts 字段可以定义快捷命令:
npm run start相当于node appnpm run dev可以用 nodemon 自动重启npm test运行测试
json
{
"name": "5-npm-scripts",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {
"nodemon": "^1.9.2"
},
"scripts": {
"start": "node app",
"dev": "nodemon app",
"test": "node test",
"test-watch": "nodemon test"
}
}nodemon
nodemon 可以监控 JS 文件变化,自动重启 Node.js 进程。
bash
npm install nodemon -D示例:模块 + 测试
app.js
javascript
'use strict';
var math = require('./math');
var value = math.add(5, 6);
console.log(value);math.js
javascript
exports.add = function(a, b) {
return a + b;
}
exports.subtract = function(a, b) {
return a - b;
}
exports.multiply = function(a, b) {
return a * b;
}test.js
javascript
'use strict';
var assert = require('assert');
var math = require('./math');
assert(math.add(3, 4) === 7);
assert(math.subtract(3, 4) === -1);
assert(math.multiply(3, 4) === 12);
console.log('all tests passed!');