eslint - 找不到模块 'eslint-plugin-angular'

标签 eslint

我正在尝试将 ESLint 集成到我的项目中,但在运行 $ eslint *.js 时遇到错误:“找不到模块 'eslint-plugin-angular'” .

我可以让它运行的唯一方法是全局安装 eslint-plugin-angular,但这似乎是错误的(除非我错过了什么??)。

我在跑:

  • 节点 v0.12.7
  • npm 3.3.4
  • eslint v1.5.0
  • eslint-plugin-angular 0.12.0

  • ESLint 是全局安装的。

    我的 package.json文件具有以下条目:
  • "eslint": "^1.5.0",
  • "eslint-plugin-angular": "^0.12.0",

  • 我有 $ 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/

    相关文章:

    javascript - Atom linter/es-lint 禁用一些警告

    eslint 错误地将 "export x from y"修复为 "exportxfrom y"

    webpack - 如何让 eslint 导入/首先理解我的 webpack 别名?

    reactjs - 使用样式化组件和 Material UI : `React does not recognize the ` showText` prop on a DOM element` 时出现 Typescript 和 eslint 问题

    javascript - ESLint 不忽略 .eslintrc.json 中给出的全局变量

    node.js - husky+lint-staged 在未暂存的文件上运行

    javascript - 如何自动检查意外遗留在 Mocha 规范中的 '.only' 调用?

    node.js - ESLint 与 Mocha

    javascript - VSCode ESLint 异步函数不支持

    javascript - 警告 ESLint 规则下出现意外的未命名函数 func-names