node.js - npm 多个命令执行 && 不起作用,但是 & 和 ||做

标签 node.js bash npm cypress

我一直在尝试运行多个 npm 命令来依次运行我的一些 cypress 测试。经过一番研究,我发现在 npm 命令之间使用 &&。

package.json 文件,在脚本中我定义了一个测试,

"scripts":{
"test":"npm run cypresscommand_1 && npm run cypresscommand_2"
}

当我出于某种原因使用 npm run test 执行此操作时,npm run cypresscommand_1 被执行但 npm run cypresscommand_2 没有被执行。随着进一步的研究和一些教程的学习,后来我尝试了两个修改

修改 1:我用 && 的 insted

"scripts":{
"test":"npm run cypresscommand_1 & npm run cypresscommand_2"
}

修改 2:我使用了 && 的 insted ||

"scripts":{
"test":"npm run cypresscommand_1 || npm run cypresscommand_2"
}

令人惊讶的是,两者都给了我预期的结果,这意味着既执行了 npm run cypresscommand_1,又执行了 npm run cypresscommand_2

我想知道的是,

  1. 在新版本的 npm 中,他们是否将 && 替换为 & 和 ||
  2. &的意思是等于和
  3. 是||的意思等于和
  4. &和||有什么区别

虽然代码运行良好,但我想确定我是否使用了正确的语法。 有人可以帮忙吗?

谢谢。

最佳答案

它与 npm 无关,这些在基于 Linux 的系统上被解释为 bash 命令,

& - 表示它将作为后台作业运行,

&& - 每个命令的退出代码并将其用作链式 && 操作中的操作数。

| - 是一个管道运算符,其中一个命令的输出将传递给以下命令,

|| - OR 逻辑运算符,如果一对中只有一个完成,则让 Bash 继续处理链式命令。

对于您的情况,您可以尝试使用分号 ';'即使失败,它也会一个接一个地运行命令。

"scripts":{ "test":"npm run cypresscommand_1 ; npm run cypresscommand_2"}

关于node.js - npm 多个命令执行 && 不起作用,但是 & 和 ||做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62820197/

相关文章:

node.js - TypeScript 中是否有等效的 require().default ?

bash - 通过管道时为脚本传递参数

bash - ECS/Fargate 容器定义命令参数

node.js - 为 Atom 开发包时 Node 模块版本冲突

javascript - 在 NodeJS 的构造函数中使用 `require` 是一种不好的做法吗?

node.js - 无法使用 Google Cloud 的 Oauth2 发送 NodeMailer(响应代码 : 530)

linux - 如何从文件读取变量,修改并将其保存到另一个变量

javascript - 安装带有帮助文件的 Node 模块会导致 'cannot find module' 错误

node.js - 命令行无法识别 Node-sass

node.js - 在 Node.js 中将 csv 文件读入多维数组的具体步骤是什么?