如何在package.json文件中创建一个命令,该命令一个接一个地为终端(控制台)执行两个命令?
此问题涉及 node.js 和 NPM包。
我有“服务器”文件夹,其中我安装了 http服务器和 npm package.json文件,还有一个文件夹带有网页的“webdir”(在“服务器”文件夹中)。
我可以手动运行Web服务器,也可以输入终端:node [path to the file that runs the server]
,但我需要在命令npm start
上自动执行此操作。并且必须从“webdir”文件夹激活服务器,以便我可以立即转到localhost: 8080
的网页。
为此,我需要在终端(控制台)中依次写入两个命令:
cd webdir
node [path to the file that runs the Web server]
如何制作一个npm命令,在控制台中执行两个命令 (一个接一个)?
以下是使用单行脚本的工作代码:
"scripts": {
"start": "node (...)/node_modules/http-server/bin/http-server"
},
答案 0 :(得分:3)
我所知道的语法涉及使用&&
分隔命令,即
"cd webdir && node ..."
这应该适用于Windows和基于Unix的系统。
例如,jQuery在其部署脚本中使用这样的组合语句:
"build": "npm install && grunt",
"start": "grunt watch",
"test": "grunt && grunt test:slow",
"precommit": "grunt lint:newer",
"commitmsg": "node node_modules/commitplease"
请参阅https://github.com/jquery/jquery/blob/master/package.json
答案 1 :(得分:1)
在第一个CLI语句后尝试使用分号,然后在分号后使用第二个语句。