如何制作一个npm命令,在控制台中执行两个命令(一个接一个)? [英] How to make a npm command, that executes two commands in the console (one after another)?
问题描述
如何在package.json文件中创建一个命令,该命令一个接一个地为终端(控制台)执行两个命令?
How can I make a command in the package.json file, that executes two commands for the terminal (console) one after another?
此问题涉及 node.js 和 NPM包。
我有server文件夹,其中我安装了 http服务器和 npm package.json文件,还有一个文件夹webdir(在server文件夹中)带有网页。
I have a "server" folder in which I installed http server and npm package.json file, and there is also a folder "webdir" (inside "server" folder) with a web page.
我可以手动运行Web服务器,也可以输入terminal: node [运行服务器的文件的路径]
,但我需要在命令 npm start
。并且必须从webdir文件夹激活服务器,以便我可以立即转到我的网页 localhost:8080
。
I can run the web server manually, or by typing in the terminal: node [path to the file that runs the server]
, but I need this to happen automatically on command npm start
. And the server must be activated from the "webdir" folder, so that I could immediately go to my web page at localhost: 8080
.
为此,我需要一个接一个地在终端(控制台)中写两个命令:
To do this, I need to write in the terminal (console) two commands one after another:
-
cd webdir
-
node [运行Web服务器的文件的路径]
cd webdir
node [path to the file that runs the Web server]
如何在控制台中执行npm命令,执行两个命令 (一个接一个)?
How to make a npm command, that executes two commands in the console (one after another)?
以下是包含单行脚本的工作代码:
Here is the working code with a single line script:
"scripts": {
"start": "node (...)/node_modules/http-server/bin/http-server"
},
推荐答案
我所知道的语法涉及到使用&&
分隔命令,即
The syntax I'm aware of involves using &&
to separate the commands, i.e.
"cd webdir && node ..."
d在Windows和基于Unix的系统上都可以工作。
This should work both on Windows and on Unix based systems.
例如,jQuery在其部署脚本中使用这样的组合语句:
For example, jQuery uses such combined statements in its deploy scripts:
"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
这篇关于如何制作一个npm命令,在控制台中执行两个命令(一个接一个)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!