【问题标题】:How to make a npm command, that executes two commands in the console (one after another)?如何制作一个 npm 命令,在控制台中执行两个命令(一个接一个)?
【发布时间】:2016-08-13 00:06:32
【问题描述】:

如何在 package.json 文件中创建一个命令,依次为终端(控制台)执行两个命令?


本题参考node.jsNPM包

我有一个“server”文件夹,里面安装了http servernpm package.json文件,还有一个文件夹"webdir"(在 "server" 文件夹内)和网页。

我可以手动运行 Web 服务器,也可以在终端中输入:node [path to the file that runs the server],但我需要在命令 npm start 上自动运行。并且必须从 "webdir" 文件夹激活服务器,以便我可以立即访问我的网页 localhost: 8080

为此,我需要在终端(控制台)中依次写入两个命令:

  1. cd webdir
  2. node [path to the file that runs the Web server]

如何制作一个 npm 命令,在控制台中执行两个命令(一个接一个)

这是带有单行脚本的工作代码:

"scripts": {
    "start": "node (...)/node_modules/http-server/bin/http-server"
  },

【问题讨论】:

  • 可能会使用分号来破坏命令,例如cd /where/ever ; node /some/file

标签: javascript json node.js http npm


【解决方案1】:

我知道的语法涉及使用&& 来分隔命令,即

"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

【讨论】:

  • 非常感谢,它有效!分号也可以! :)
【解决方案2】:

在第一个 CLI 语句后尝试使用分号,然后在分号后尝试第二个语句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    • 2016-01-29
    • 2017-12-31
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 2016-01-19
    相关资源
    最近更新 更多