我在使用 yarn
提供的 run
命令时遇到问题,yarn
是 Facebook 的 JavaScript 包管理器。
目前在我的 package.json
文件中,我的 scripts
对象有以下内容。
"scripts": {
"lint": "./node_modules/.bin/eslint --ignore-pattern dist ."
}
当我运行以下命令时,它按预期工作,npm run lint
。但是,当我使用 yarn run lint
从 yarn
运行脚本时,我收到以下错误。
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
上,我确实注意到,如果您有类似 date
或pwd
然后 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
一样运行命令。
最佳答案
我发现了一个我认为相关的问题:https://github.com/yarnpkg/yarn/pull/809 .
yarn 不理解 npm 脚本中的空格。我想这将在下一个版本中修复。我可以在我的电脑上重现这个问题(使用最新的 yarn :0.15.1)。
顺便说一下,您不必在 npm 脚本中包含 ./node_modules/.bin
。默认情况下,npm 和 yarn 都会查看该文件夹,如下所述:https://docs.npmjs.com/cli/run-script
所以你的脚本只能是:"lint": "eslint --ignore-pattern dist ."
关于javascript - 无法在 package.json 中的脚本对象上运行 "yarn run",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40049508/