我想实现可以在npm中运行两个命令的情况:
npm start
npm startDev
我以这种方式配置package.json
{
"name": "Test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "SET NODE_ENV=production & nodemon servis.js",
"startdev": "nodemon servis.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.3",
"morgan": "^1.9.0",
"nodemon": "^1.18.3"
}
}
运行 npm start 可以正常运行,但是当我键入 npm startDev 时,我会得到
Usage: npm <command>
where <command> is one of:
access, adduser, bin, bugs, c, cache, completion, config,
ddp, dedupe, deprecate, dist-tag, docs, doctor, edit,
explore, get, help, help-search, i, init, install,
install-test, it, link, list, ln, login, logout, ls,
outdated, owner, pack, ping, prefix, profile, prune,
publish, rb, rebuild, repo, restart, root, run, run-script,
s, se, search, set, shrinkwrap, star, stars, start, stop, t,
team, test, token, tst, un, uninstall, unpublish, unstar,
up, update, v, version, view, whoami
答案 0 :(得分:2)
文档中定义了许多preset
命令,例如start:
这将运行软件包“开始”中指定的任意命令 “脚本”对象的属性。如果未指定“开始”属性 在“脚本”对象上,它将运行节点server.js。
如果您想添加一个不是preset
的新命令(例如startDev
),则可以像直接操作一样直接在package.json
中添加它:
{
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "SET NODE_ENV=production & nodemon servis.js",
"startdev": "nodemon servis.js"
},
}
但是要运行它,您必须使用npm run
这从程序包的“脚本”对象运行任意命令。如果 没有提供“命令”,它将列出可用的脚本。 run [-script]由测试,启动,重新启动和停止命令使用, 但也可以直接调用。打包脚本时 打印出来,将它们分为生命周期(测试,开始, 重新启动)并直接运行脚本。
示例:
npm run startdev