无法在package.json中的脚本对象上运行"yarn run"

Pet*_*sta 6 javascript yarnpkg

我在使用Facebook的JavaScript包管理器run提供的命令时遇到了麻烦yarn.

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

"scripts": {
  "lint": "./node_modules/.bin/eslint --ignore-pattern dist ."
}
Run Code Online (Sandbox Code Playgroud)

当我运行以下命令时,它按预期工作npm run lint.但是,当我从运行脚本时yarn,yarn 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.
Run Code Online (Sandbox Code Playgroud)

./node_modules/.bin目录在我的上面$PATH,我注意到如果你有一个可执行文件,date或者pwd然后yarn run some_script_on_date将按预期工作.

使其工作的一种方法是创建一个单独的shell文件,其中包含您尝试执行的命令.我们称之为./scripts/lint.sh.

#!/bin/bash

./node_modules/.bin/eslint --ignore-pattern dist .
Run Code Online (Sandbox Code Playgroud)

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

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

"new_lint": "./scripts/lint.sh"
Run Code Online (Sandbox Code Playgroud)

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

我错过了什么,或者这是如何调用脚本yarn需要完成的?我想像运行命令一样npm.

Cor*_*ewe 3

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

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

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

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