【问题标题】:Trouble running "yarn run" on scripts object in package.json在 package.json 中的脚本对象上运行“纱线运行”时遇到问题
【发布时间】:2016-10-14 18:07:01
【问题描述】:

我在使用 Facebook 的 JavaScript 包管理器 yarn 提供的 run 命令时遇到问题。

目前在我的package.json 文件中,我的scripts 对象有以下内容。

"scripts": {
  "lint": "./node_modules/.bin/eslint --ignore-pattern dist ."
}

当我运行以下命令时,它按预期工作,npm run lint。但是,当我从 yarnyarn run lint 运行脚本时,我收到以下错误。

Petesta :: λ -> ~/Git/yarn yarn run lint
yarn run v0.15.1
$ "./node_modules/.bin/eslint --ignore-pattern dist ."
sh: ./node_modules/.bin/eslint --ignore-pattern dist .: No such file or directory
error Command failed with exit code 127.
info Visit http://yarnpkg.com/en/docs/cli/run for documentation about this command.

./node_modules/.bin 目录在我的$PATH 上,我确实注意到如果您有像datepwd 这样的可执行文件,那么yarn run some_script_on_date 将按预期工作。

实现此功能的一种方法是创建一个单独的 shell 文件,其中包含您尝试执行的命令。我们就叫它./scripts/lint.sh吧。

#!/bin/bash

./node_modules/.bin/eslint --ignore-pattern dist .

然后在文件chmod +x ./scripts/lint.sh上运行这个命令

现在在您的 scripts 对象中添加以下行。

"new_lint": "./scripts/lint.sh"

现在yarn run new_lint 将按预期运行。

我是否遗漏了什么,或者这是否需要在yarn 中调用脚本?我想像npm一样运行命令。

【问题讨论】:

    标签: javascript yarnpkg


    【解决方案1】:

    我发现了一个我认为相关的问题:https://github.com/yarnpkg/yarn/pull/809

    yarn 不理解 npm 脚本中的空格。我想这将在下一个版本中修复。我可以在我的电脑上重现这个问题(使用最新的纱线:0.15.1)。

    顺便说一句,您不必在 npm 脚本中包含 ./node_modules/.bin。默认情况下,npm 和 yarn 都会在该文件夹中查看,如下所述:https://docs.npmjs.com/cli/run-script

    所以你的脚本只能是:"lint": "eslint --ignore-pattern dist ."

    【讨论】:

    • 我用eslint 尝试过,但它没有那样工作。找不到该命令。这适用于npm run,但不适用于yarn run
    • 从字面上看,前几天,这个包被称为yarnpkg,最近才被yarn弃用。在此弃用之前,yarnpkg run command 按预期工作。现在有了新包,它就不起作用了。
    • 啊,我误会了。只需要等待发布。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 2011-06-01
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多