如何使用NPM脚本在package.json中启动运行/启动json服务器和实时服务器?

时间:2020-07-02 17:20:43

标签: npm package.json json-server npm-live-server

我有一个包含html / css / javascript文件的项目,我想输入命令npm start来运行json-serverlive-server

我尝试了以下命令:"start": "json-server --watch db.json && live-server",但它仅启动第一个命令,而不同时启动两个命令。

package.json

{
  "name": "testa-npm-live-server",
  "version": "1.0.0",
  "description": "",
  "main": "",
  "dependencies": {},
  "devDependencies": {
    "json-server": "^0.16.1",
    "live-server": "^1.2.1"
  },
  "scripts": {
    "start": "json-server --watch db.json && live-server"
  },
  "author": "",
  "license": "ISC"
}

IDE:vscode
节点版本:v12.18.1
NPM版本:6.14.5

1 个答案:

答案 0 :(得分:0)

使用concurrently paralell 中运行命令。 &&以串行/顺序方式运行,即第二条命令仅在第一个命令运行完毕后才开始。

{
  "name": "testa-npm-live-server",
  "version": "1.0.0",
  "description": "",
  "main": "",
  "dependencies": {},
  "devDependencies": {
    "concurrently": "^5.2.0",
    "json-server": "^0.16.1",
    "live-server": "^1.2.1"
  },
  "scripts": {
    "start": "concurrently \"json-server --watch db.json\" \"live-server\""
  },
  "author": "",
  "license": "ISC"
}

非常感谢NodeSchool Campinas社区的帮助:)