4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

npm-run-all を使用すれば、sever起動時のロックを防いでワンライナーで複数処理が実行できる

Last updated at Posted at 2019-04-18

TL;DR

  • npm-run-allを使用することで、サーバー起動時の Listening処理により発生するロックの影響を受けず、別処理を実行することができる
  • scriptもスッキリと短く記入できる

経緯

モックサーバーを:3000、ローカルサーバー(クライアント側のエントリーポイント)を:8000で、ワンライナーで立ち上げたかったのですが、
npm run <Mock Run> && <Local-Server Run> とすると、Mockの方でリスニング状態になってしまい、 ローカルサーバーの起動が実行されない。
デフォルトのスクリプトでは、ワンライナーではサーバーを並列実行できない ? → 導入しました。

install

npm-run-all こちらの公式サイトを参考に npm install.

コマンド 一覧

npm-run-all

公式より
The main command is npm-run-all. We can make complex plans with npm-run-all command.
Both run-s and run-p are shorthand commands. run-s is for sequential, run-p is for parallel. We can make simple plans with those commands.

  • 日本語訳
    • メインコマンドはnpm-run-allだよ。
    • run-sコマンドとrun-pコマンドはショートハンドで、
      • run-sは直列(連続)実行
      • run-pは並列(同時)実行

実例

モックサーバーの立ち上げと、webpackのホットリロードを同時に立ち上げてみました。

npm run devで、npm run apinpm run serveを同時に実行しています。

package.json
{
    "scripts": {
      "serve": "vue-cli-service serve", 
      "build": "vue-cli-service build",
      "lint": "vue-cli-service lint",
      "test:unit": "vue-cli-service test:unit",
      "api": "npx json-server --watch db.json",
      "dev": "run-p api serve"
  },
}
4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?