Unable to resolve relative module paths when eslint run from sub-folder

时光怂恿深爱的人放手 提交于 2021-01-27 02:37:45

问题


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.:

This will work:

cd /reporoot
eslint .

All of these commands will fail with error shown above:

cd subfolder0
eslint .

or

eslint subfolder0

or

eslint /reporoot/subfolder0/subfolder1/MyFile.js

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


回答1:


This is probably a problem with absolute paths in your imports according to this GitHub issue.

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.



来源:https://stackoverflow.com/questions/56064609/unable-to-resolve-relative-module-paths-when-eslint-run-from-sub-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!