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

标签 javascript yarnpkg

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

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

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

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

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 一样运行命令。

最佳答案

我发现了一个我认为相关的问题: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/

相关文章:

javascript - 滚动事件上的 jQuery .addClass() 不起作用

javascript - node.js 服务器端的监听器太多?

yarnpkg - yarn 进度指示器代表什么?

node.js - yarn 安装中的意外 token

node.js - 与 npm 和 yarn 一起使用的 package.json 脚本?

vue.js - 如何在没有 monorepo 的情况下在多个 vue 应用程序中组织代码(组件)共享

javascript - 如果页面上不可用,则从托管的 javascript 文件动态加载 jQuery

javascript - 两个脚本之间的jquery冲突

Javascript function.prototype.apply() 使用字符串而不是 `this`

yarnpkg - 如何判断一个项目是否使用了 yarn