我正在尝试将 ESLint 集成到我的项目中,但在运行 $ eslint *.js
时遇到错误:“找不到模块 'eslint-plugin-angular'” .
我可以让它运行的唯一方法是全局安装 eslint-plugin-angular,但这似乎是错误的(除非我错过了什么??)。
我在跑:
ESLint 是全局安装的。
我的
package.json
文件具有以下条目:我有
$ rm -rf node_modules
并重新运行 $ npm install
确保没有某种 npm 依赖问题或损坏。我还验证了 node_modules
中的目录权限以确保可以访问。我的
.eslintrc
包含:{
"plugins": [
"angular"
],
"rules": {
"angular/ng_controller_name": [
2,
"/[A-Z].*Controller$/"
],
"quotes": [
2,
"single"
]
},
"globals": {
"angular": true
}
}
我在这里想念什么?谢谢!
附言。我确实在github上开了一张票:https://github.com/Gillespie59/eslint-plugin-angular/issues/222 - 只是想知道 Stack 社区中是否有人遇到过这个问题。
最佳答案
如果您遵循告诉您使用 npm install -g eslint
的指南,您几乎肯定会遇到这个问题。 ,这是安装 ESLint 的推荐方式,然后继续立即告诉您使用 eslint --init
为您的项目创建配置。问题是 eslint --init
将安装您需要的软件包,但它会在本地安装它们,即使它是从全局安装的 eslint
运行的.在我尝试在 Emacs 和 flycheck
中编辑文件之前,我一点也不聪明。从 ESLint 报错:
Error: Failed to load plugin react: Cannot find module 'eslint-plugin-react'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
[...]
我喜欢同时允许全局安装和本地安装。我可能会将全局代码用于我仍想检查但不属于任何特定项目的实验代码,以及具有更具体需求的本地安装。所以我创建了一个存储为
~/bin/eslint
的脚本.由于~/bin
早在我的PATH
,此脚本用于代替任何全局安装 eslint
:#!/bin/sh
# Find the top of the code hierarchy, and move there.
prefix=`npm prefix`
[ -n $prefix ] && cd $prefix
# Execute the local eslint if it exists.
local_eslint=./node_modules/.bin/eslint
[ -x $local_eslint ] && exec $local_eslint "$@"
# If not, execute the global one.
exec /path/to/global/eslint "$@"
(对于那些不知道的人,
exec
是最终的。如果第一个 exec
被执行,那么它之后的任何内容都不会执行。)该脚本采用适合
npm
的项目结构。 (即有一个 package.json
可以找到 npm prefix
)。它必须适应其他情况。我的全局
eslint
位于不寻常的位置,请调整脚本以将其指向您安装它的位置。
关于eslint - 找不到模块 'eslint-plugin-angular',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32683223/