And*_*ena 469 javascript build node.js
在我的package.json我有这两个脚本:
"scripts": {
"start-watch": "nodemon run-babel index.js",
"wp-server": "webpack-dev-server",
}
Run Code Online (Sandbox Code Playgroud)
每次我开始在Node.js中开发时,我必须并行运行这两个脚本.我想到的第一件事就是添加第三个脚本:
"dev": "npm run start-watch && npm run wp-server"
Run Code Online (Sandbox Code Playgroud)
......但是start-watch在运行之前会等待完成wp-server.
我怎样才能并行运行这些?请记住,我需要查看output这些命令.此外,如果您的解决方案涉及构建工具,我宁愿使用gulp而不是grunt因为我已经在另一个项目中使用它.
Nei*_*ner 543
使用一个名为concurrently的包.
npm i concurrently --save-dev
然后设置您的npm run dev任务:
"dev": "concurrently --kill-others \"npm run start-watch\" \"npm run wp-server\""
Run Code Online (Sandbox Code Playgroud)
Pat*_*ell 322
使用Concurrently包工作,但您不需要它来完成此任务.您可以在基于UNIX的计算机上使用管道来运行并发任务.我建议使用这种方法,因为它可以使您不必添加额外的依赖项.
"dev": "npm run start-watch > /dev/null | npm run wp-server"
Run Code Online (Sandbox Code Playgroud)
Dio*_*oso 106
如果您使用的是类UNIX环境,&请将其用作分隔符:
"dev": "npm run start-watch & npm run wp-server"
Run Code Online (Sandbox Code Playgroud)
否则,如果您对跨平台解决方案感兴趣,可以使用npm-run-all 模块:
"dev": "npm-run-all --parallel start-watch wp-server"
Run Code Online (Sandbox Code Playgroud)
o.v*_*.v. 69
从windows cmd你可以使用start:
"dev": "start npm run start-watch && start npm run wp-server"
Run Code Online (Sandbox Code Playgroud)
以这种方式启动的每个命令都在自己的窗口中启动
小智 54
您应该使用npm-run-all(或concurrently,parallelshell),因为它可以更好地控制启动和终止命令.运营商&,|是坏的想法,因为你需要手动停止所有的测试都完成之后.
这是通过npm进行量角器测试的一个例子:
scripts: {
"webdriver-start": "./node_modules/protractor/bin/webdriver-manager update && ./node_modules/protractor/bin/webdriver-manager start",
"protractor": "./node_modules/protractor/bin/protractor ./tests/protractor.conf.js",
"http-server": "./node_modules/http-server/bin/http-server -a localhost -p 8000",
"test": "npm-run-all -p -r webdriver-start http-server protractor"
}
Run Code Online (Sandbox Code Playgroud)
-p =并行运行命令.
-r =当其中一个命令以退出代码零结束时,终止所有命令.
运行npm run test将启动Selenium驱动程序,启动http服务器(为您提供文件)并运行量角器测试.完成所有测试后,它将关闭http服务器和selenium驱动程序.
Cor*_*rey 24
更好的解决方案是使用 &
"dev": "npm run start-watch & npm run wp-server"
Run Code Online (Sandbox Code Playgroud)
Beh*_*adi 16
您可以使用一个&并行运行脚本
"dev": "npm run start-watch & npm run wp-server"
Run Code Online (Sandbox Code Playgroud)
Ent*_*ack 15
我有一个没有任何附加模块的跨平台解决方案.我正在寻找类似try catch块的东西,我可以在cmd.exe和bash中使用它们.
解决方案command1 || command2似乎在两个环境中都有效.所以OP的解决方案是:
"scripts": {
"start-watch": "nodemon run-babel index.js",
"wp-server": "webpack-dev-server",
// first command is for the cmd.exe, second one is for the bash
"dev": "(start npm run start-watch && start npm run wp-server) || (npm run start-watch & npm run wp-server)",
"start": "npm run dev"
}
Run Code Online (Sandbox Code Playgroud)
那么简单npm start(和npm run dev)将适用于所有平台!
Edw*_*rey 14
使用并发可与共享输出流并行运行命令。为了方便区分哪个输出来自哪个进程,请使用缩短的命令形式,例如npm:wp-server. 这会导致同时为每个输出行添加其命令名称前缀。
在 中package.json,您的脚本部分将如下所示:
"scripts": {
"start": "concurrently \"npm:start-watch\" \"npm:wp-server\"",
"start-watch": "nodemon run-babel index.js",
"wp-server": "webpack-dev-server"
}
Run Code Online (Sandbox Code Playgroud)
Dar*_*wic 12
我已经检查了几乎所有上面的解决方案,只有npm-run-all我才能解决所有问题.与所有其他解决方案相比的主要优点是能够使用参数运行脚本.
{
"test:static-server": "cross-env NODE_ENV=test node server/testsServer.js",
"test:jest": "cross-env NODE_ENV=test jest",
"test": "run-p test:static-server \"test:jest -- {*}\" --",
"test:coverage": "npm run test -- --coverage",
"test:watch": "npm run test -- --watchAll",
}
Run Code Online (Sandbox Code Playgroud)
注意
run-p是快捷方式npm-run-all --paraller
这允许我使用像这样的参数运行命令npm run test:watch -- Something.
编辑:
还有一个有用的选项为npm-run-all:
-r, --race - - - - - - - Set the flag to kill all tasks when a task
finished with zero. This option is valid only
with 'parallel' option.
Run Code Online (Sandbox Code Playgroud)
添加-r到您的npm-run-all脚本以在完成代码时终止所有进程0.当您运行HTTP服务器和使用该服务器的另一个脚本时,这尤其有用.
"test": "run-p -r test:static-server \"test:jest -- {*}\" --",
Run Code Online (Sandbox Code Playgroud)
Yil*_*maz 11
随着安装npm install concurrently
"scripts": {
"start:build": "tsc -w",
"start:run": "nodemon build/index.js",
"start": "concurrently npm:start:*"
},
Run Code Online (Sandbox Code Playgroud)
运行多个 Node 脚本的另一种选择是使用单个 Node 脚本,它可以派生许多其他脚本。在 Node 中本机支持分叉,因此它不添加任何依赖项并且是跨平台的。
这只会按原样运行脚本并假设它们位于父脚本的目录中。
// fork-minimal.js - run with: node fork-minimal.js
const childProcess = require('child_process');
let scripts = ['some-script.js', 'some-other-script.js'];
scripts.forEach(script => childProcess.fork(script));
Run Code Online (Sandbox Code Playgroud)
这将运行带有参数并由许多可用选项配置的脚本。
// fork-verbose.js - run with: node fork-verbose.js
const childProcess = require('child_process');
let scripts = [
{
path: 'some-script.js',
args: ['-some_arg', '/some_other_arg'],
options: {cwd: './', env: {NODE_ENV: 'development'}}
},
{
path: 'some-other-script.js',
args: ['-another_arg', '/yet_other_arg'],
options: {cwd: '/some/where/else', env: {NODE_ENV: 'development'}}
}
];
let runningScripts= [];
scripts.forEach(script => {
let runningScript = childProcess.fork(script.path, script.args, script.options);
// Optionally attach event listeners to the script
runningScript.on('close', () => console.log('Time to die...'))
runningScripts.push(runningScript); // Keep a reference to the script for later use
});
Run Code Online (Sandbox Code Playgroud)
分叉还有一个额外的好处,即父脚本可以从分叉的子进程接收事件以及发回。一个常见的例子是父脚本杀死其分叉的孩子。
runningScripts.forEach(runningScript => runningScript.kill());
Run Code Online (Sandbox Code Playgroud)
有关更多可用的事件和方法,请参阅ChildProcess文档
在这种情况下,我想说最好的选择 如果这个脚本是针对仅在基于*nix的机器上运行的私有模块,则可以使用控制运算符进行分叉处理,如下所示:&
在部分package.json文件中执行此操作的示例:
{
"name": "npm-scripts-forking-example",
"scripts": {
"bundle": "watchify -vd -p browserify-hmr index.js -o bundle.js",
"serve": "http-server -c 1 -a localhost",
"serve-bundle": "npm run bundle & npm run serve &"
}
Run Code Online (Sandbox Code Playgroud)
然后你可以通过并行执行它们npm run serve-bundle.您可以增强脚本以将分叉进程的pids输出到如下文件:
"serve-bundle": "npm run bundle & echo \"$!\" > build/bundle.pid && npm run serve & echo \"$!\" > build/serve.pid && npm run open-browser",
Run Code Online (Sandbox Code Playgroud)
Google就像bash控制运算符一样,可以分析它如何运作.我还提供了一些关于在以下Node项目中利用Unix技术的进一步背景:
如果您不在Windows上,Unix工具/技术通常可以很好地实现Node脚本,因为:
在Nodeland系统任务模块也往往抽象或Unix工具近似,从fs到streams.
npm-run-all --parallel task1 task2
Run Code Online (Sandbox Code Playgroud)
编辑:
您需要预先安装npm-run-all.另请查看此页面了解其他使用方案.
使用 npm 运行多个并行脚本的分步指南。全局安装npm-run-all包
npm i -g npm-run-all
Run Code Online (Sandbox Code Playgroud)
现在安装此包并将其保存在package.json所在的项目中
npm i npm-run-all --save-dev
Run Code Online (Sandbox Code Playgroud)
现在以这种方式修改package.json文件中的脚本
"scripts": {
"server": "live-server index.html",
"watch": "node-sass scss/style.scss --watch",
"all": "npm-run-all --parallel server watch"
},
Run Code Online (Sandbox Code Playgroud)
现在运行这个命令
npm run all
Run Code Online (Sandbox Code Playgroud)
有关此包的更多详细信息,请参见给定链接 npm-run-all
| 归档时间: |
|
| 查看次数: |
293462 次 |
| 最近记录: |