首页 > 解决方案 > 调用其他 Yarn 脚本时,纱线脚本失败

问题描述

从 Yarn 脚本调用其他 Yarn 脚本时遇到问题。我想要实现的是yarn develop在启动几个观察者并运行我的应用程序之前首先调用 TypeScript 和其他一些预处理器的任务。我想使用npm-run-all(或类似的东西)调用多个目标,因为这将允许我定义单独的目标,我也可以手动运行这些目标,而package.json.

我制作了一个最小的复制案例,其中包含以下 package.json:

{
  "name": "test",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "task:one": "echo one",
    "task:two": "echo two",
    "run-all": "npm-run-all task:one task:two",
    "run-all-yarn": "yarn task:one && yarn task:two"
  },
  "devDependencies": {
    "npm-run-all": "^4.1.5"
  }
}

正如预期的那样,调用yarn task:one会产生. 如果我调用,我会得到一个错误:echo oneyarn task:twoyarn run-all

$-> yarn run-all
yarn run v1.22.10
$ npm-run-all task:one task:two
$ echo one
error Couldn't find the binary echo one
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
ERROR: "task:one" exited with 1.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

yarn run-all-yarn失败(几乎完全正确,没有在第二行执行的命令)相同的错误。

如果我使用 NPM ( npm run run-all) 而不是 Yarn,则一切都按预期工作:

$-> npm run run-all

> test@1.0.0 run-all /Users/basdalenoord/Projects/52DN/psyflix/test
> npm-run-all task:one task:two


> test@1.0.0 task:one /Users/basdalenoord/Projects/52DN/psyflix/test
> echo one

one

> test@1.0.0 task:two /Users/basdalenoord/Projects/52DN/psyflix/test
> echo two

two

我正在使用我的节点版本从 nvm 提供的 Yarn 版本 1.22.17:~/.nvm/versions/node/v14.18.1/bin/yarn

对于为什么运行单个目标会很好但将它们组合起来对于纱线来说失败,而相同的脚本适用于npm.

标签: npmyarnpkg

解决方案


推荐阅读