从子文件夹运行eslint时无法解析相对的模块路径 [英] Unable to resolve relative module paths when eslint run from sub-folder

查看:146
本文介绍了从子文件夹运行eslint时无法解析相对的模块路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从仓库的根目录运行eslint时,一切运行正常,没有错误.

When I run eslint from the root folder of my repo everything runs fine, with no errors.

但是,当我从子文件夹运行时,会得到大量导入/无法解析的信息,而从根目录运行时不会发生这种情况:

But when I run from a subfolder I get a ton of import/no-unresolved that don't happen when I run from root:

/reporoot/subfolder0/subfolder1/MyFile.js
  11:8   error  Unable to resolve path to module 'foo'  import/no-unresolved
  11:8   error  Missing file extension for "foo"        import/extensions
  14:97  error  Unable to resolve path to module 'foo'  import/no-unresolved
  14:97  error  Missing file extension for "foo"        import/extensions

我怎么跑都没关系.例如:

Doesn't matter how I run. e.g.:

这将起作用:

cd /reporoot
eslint .

所有这些命令将失败,并显示上面的错误:

All of these commands will fail with error shown above:

cd subfolder0
eslint .

eslint subfolder0

eslint /reporoot/subfolder0/subfolder1/MyFile.js

任何想法是什么问题,或有关如何解决它的想法?

Any idea what the issue is, or thoughts about how to fix it?

推荐答案

根据尝试在您的 .eslintrc.json 中进行设置(如果不使用JSON,则将其转换为任何其他文件类型)

Try to set this in your .eslintrc.json (Or convert it to any other filetype if you don't use JSON)

"settings": {
    "import/resolver": {
        "node": {
            "extensions": [".js", ".jsx", ".ts", ".tsx"]
        }
    }
}

如果上述方法不起作用,而您只是想摆脱这些错误,则只需添加以下内容即可:

If the above doesn't work and you just want to get rid of these errors just add this instead:

"settings": {
    "import/no-unresolved": 0, // Turn off "Unable to resolve path to module ..." error
    "import/extensions": 0 // Turn off "Missing file extension for ..." error
}

无论如何,禁用它都没什么大不了的,因为众所周知,短绒棉因路径问题而闻名.

Disabling this is not that big of a deal anyway because linters are pretty well known for having problems with pathing.

这篇关于从子文件夹运行eslint时无法解析相对的模块路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆