Eslint-plugin-import: Unable to resolve path to module (import/no-resolved) only in Sublime text 3

Created on 4 Dec 2015  ยท  41Comments  ยท  Source: benmosher/eslint-plugin-import

I have an Error "Unable to resolve path to module" (import/no-resolved) only in Sublime text 3 for any of my es6 module import statements. At the same time, I have no problems with bundling, and properly working highlighted export of multiple variables, e.g. :

export { export1, export2, export3 };

and other eslint-plugin-import rules
I have node 5.0.0 installed via nvm, all dependency modules removed, and npm i
this is totally wierd( I kill 2 days for it.

can't use "eslint-import-resolver-webpack": "^0.1.4", because I use gulp-webpack plugin with configuration in gulpfile.js instead of separate webpack.config.js , and have several pages in build process.

I'm trying to slowly integrate ReactJS in my project, which I have frameworkless pure functional ES5, and now I'm converting it to ES6, before I can start with React and single webpack.config.js

Please, can somebody help me out? I kill 2 days desperately fight this problem.

.eslintrc

{
  "parser": "babel-eslint",
  "env": {
    "node": true,
    "browser": true,
    "commonjs": true,
    "worker": true,
    "mocha": true,
    "mongo": true,
    "es6": true
  },
  "ecmaFeatures": {
    "arrowFunctions": true,
    "blockBindings": true,
    "destructuring": true,
    "modules": true,
    "spread": true,
    "templateStrings": true,
    "forOf": true
  },
  "plugins": [
    "react",
    "import"
  ],
  "settings": {
    "ecmascript": 6,
    "jsx": true
  },
  "rules": {
    "quotes": 2,
    "no-unused-vars": 2,
    "camelcase": 0,
    "no-underscore-dangle": 0,

    "indent": [2, 2, {"VariableDeclarator": { "var": 2, "let": 2, "const": 3}}],
    "comma-dangle": [2, "never"],
    "no-dupe-args": 2,
    "no-dupe-keys": 2,
    "no-empty": 2,
    "no-extra-boolean-cast": 2,
    "no-extra-parens": 2,
    "no-extra-semi": 2,
    "no-func-assign": 2,
    "no-inner-declarations": 2,
    "no-irregular-whitespace": 2,
    "no-negated-in-lhs": 2,
    "no-sparse-arrays": 2,
    "no-unexpected-multiline": 2,
    "no-unreachable": 2,
    "use-isnan": 2,
    "valid-jsdoc": 2,
    "valid-typeof": 2,

    "block-scoped-var": 2,
    "curly": [2, "multi-line"],
    "default-case": 2,
    "guard-for-in": 2,
    "no-alert": 2,
    "no-caller": 2,
    "no-case-declarations": 2,
    "no-else-return": 2,
    "no-empty-label": 2,
    "no-empty-pattern": 2,
    "no-eq-null": 2,
    "no-eval": 2,

    "import/default": 2,
    "import/no-unresolved": 2,
    "import/named": 2,
    "import/namespace": 2,
    "import/export": 2,
    "import/no-duplicates": 2,
    "import/imports-first": 2
  },
  "syntax_map": {
    "JavaScript (Babel)": "javascript",
  }
}

package.json

"devDependencies": {
    "babel-core": "^6.2.1",
    "babel-eslint": "^4.1.6",
    "babel-loader": "^6.2.0",
    "babel-preset-es2015": "^6.1.18",
    "eslint": "^1.10.2",
    "eslint-import-resolver-webpack": "^0.1.4",
    "eslint-loader": "^1.1.1",
    "eslint-plugin-import": "^0.11.0",
    "eslint-plugin-react": "^3.11.1",
    "eslint_d": "^2.3.2",
    "gulp": "3.9.0",
    "gulp-babel": "^6.1.0",
    "gulp-webpack": "^1.5.0",
    "webpack": "^1.12.9"
  }

.sublimelinterrc

{
  "linters": {
    "eslint": {
      "args": ["--stdin-filename", "@"]
    }
  }
}

SublimeLinter.sublime-settings

{
    "user": {
        "debug": false,
        "delay": 0.25,
        "error_color": "D02000",
        "gutter_theme": "Packages/SublimeLinter/gutter-themes/Default/Default.gutter-theme",
        "gutter_theme_excludes": [],
        "lint_mode": "background",
        "linters": {
            "eslint": {
                "@disable": false,
                "args": [],
                "excludes": []
            },
            "eslint_d": {
                "@disable": false,
                "args": [],
                "excludes": []
            }
        },
        "mark_style": "outline",
        "no_column_highlights_line": false,
        "passive_warnings": false,
        "paths": {
            "linux": [],
            "osx": [
                "/Users/justfly/.nvm/versions/node/v5.0.0/bin/node"
            ],
            "windows": []
        },
        "python_paths": {
            "linux": [],
            "osx": [],
            "windows": []
        },
        "rc_search_limit": null,
        "shell_timeout": 10,
        "show_errors_on_save": false,
        "show_marks_in_minimap": true,
        "syntax_map": {
            "html (django)": "html",
            "html (rails)": "html",
            "html 5": "html",
            "javascript (babel)": "javascript",
            "magicpython": "python",
            "php": "html",
            "python django": "python"
        },
        "warning_color": "DDB700",
        "wrap_find": true
    }
}

Most helpful comment

@adrianmcli I just figured out the issue I had.
My projectRoot/.eslintrc file

"settings": {
  "import/resolver": {
    "webpack": {
      "config": "./webpack/webpack.eslint.js"
    }
  }
}

Originally in my projectRoot/webpack/webpack.eslint.js file, I had

const path = require('path');

module.exports = {
  context: process.cwd(),
  resolve: {
    modules: [
      path.resolve('./src/')
    ]
  }
};

Even though this works with Atom, it does not with Sublime.

I realized that Sublime was changing the context (process.cwd()) to the directory of the open file. Thus eslint was unable to find the local modules.
So I changed my projectRoot/webpack/webpack.eslint.js file:

const path = require('path');

module.exports = {
  resolve: {
    modules: [
      path.resolve(__dirname, '..', 'src')
    ]
  }
};

So I was able to fix the problem by providing the full path to my src directory.

All 41 comments

Have you tried the SublimeLinter instructions from the README? That would be the first step.

Also: for the webpack resolver to work, a separate config file is a must if you have any resolve or externals config. Should be straightforward to copy it out of your Gulpfile and then requireing it where it used to be?

I have 12 pages to build, so in this case I will need 12 webpack.config.js
files which difference only in entry. In future I will make single
webpack.config.js , but for now the build steps is more complicated.

I did all steps from README before I made an issue. Without this steps I
did not get unexported variables highlights.
I do not have resolve or external configs.

2015-12-06 4:39 GMT+08:00 Ben Mosher [email protected]:

Link to SublimeLinter steps;
https://github.com/benmosher/eslint-plugin-import/blob/master/README.md#sublimelinter-eslint

โ€”
Reply to this email directly or view it on GitHub
https://github.com/benmosher/eslint-plugin-import/issues/139#issuecomment-162244070
.

ohhhhh, okay, I think you probably need to move "jsx: true" from "settings" to "ecmaFeatures" in your eslintrc.

The plugin has its own dependency parser that doesn't parse JSX by default, but will respect the ecmaFeatures.jsx flag.

Also: if you find that still doesn't work, adding "import/parser": "babel-eslint" might fix it, if you're using any ES7 features.

BTW: You can specify 12 entry files in a single Webpack config, and it will build all of them.

I'm not sure how that would integrate with Gulp, though. I used to use Gulp for Webpack but I found it to be simpler to build CSS bundles with Gulp and use Webpack's CLI directly for JS. YMMV.

I know that I can specify 12 entry files , but I have sequence tasks. I
need to redo a lot of stuff in this case in gulpfile.js, and I simply do
not have time right now with my current work.
Thanx, I will try your advise tomorrow, and I will tell you if there will
be success or failure.

2015-12-07 3:53 GMT+08:00 Ben Mosher [email protected]:

BTW: You can specify 12 entry files in a single Webpack config, and it
will build all of them.

I'm not sure how that would integrate with Gulp, though. I used to use
Gulp for Webpack but I found it to be simpler to build CSS bundles with
Gulp and use Webpack's CLI directly for JS. YMMV.

โ€”
Reply to this email directly or view it on GitHub
https://github.com/benmosher/eslint-plugin-import/issues/139#issuecomment-162341626
.

Fair enough. Shortest path to success is probably adding "import/parser": "babel-eslint" to the settings in your .eslintrc.

putting "import/parser": "babel-eslint" to the settings doesn't help(
"settings": {
"ecmascript": 6,
"import/parser": "babel-eslint"
},
"ecmaFeatures": {
"jsx": true,
"arrowFunctions": true,
"blockBindings": true,
"destructuring": true,
"modules": true,
"spread": true,
"templateStrings": true,
"forOf": true
},

rule "import/no-unresolved": [2, {commonjs: true, amd: true}], still giving
me error "Unable to resolve path to module." on each import declaration.

I do not have problems in build process, only in sublime text

for now I set "import/no-unresolved": [0, {commonjs: true, amd: true}]

2015-12-07 19:29 GMT+08:00 Ben Mosher [email protected]:

Fair enough. Shortest path to success is probably adding "import/parser":
"babel-eslint" to the settings in your .eslintrc.

โ€”
Reply to this email directly or view it on GitHub
https://github.com/benmosher/eslint-plugin-import/issues/139#issuecomment-162495628
.

Hmm... I'm really not sure what might be happening. A second look through your config + what you've posted doesn't yield any more ideas... I'll keep thinking about it

Ohhh, so: you need to add "import/resolver": "webpack" to settings to use the Webpack resolver... since your config isn't in a file, I'm not sure whether it would make a difference... but it does have some different defaults and a specific bug fix that I haven't pushed to the Node resolver yet.

By now, I will not use this option. Thank you very much, keep in touch)

2015-12-07 22:55 GMT+08:00 Ben Mosher [email protected]:

Ohhh, so: you need to add "import/resolver": "webpack" to settings to use
the Webpack resolver... since your config isn't in a file, I'm not sure
whether it would make a difference... but it does have some different
defaults and a specific bug fix that I haven't pushed to the Node resolver
yet.

โ€”
Reply to this email directly or view it on GitHub
https://github.com/benmosher/eslint-plugin-import/issues/139#issuecomment-162547521
.

Can't blame you. :sweat_smile: I will close for now, let me know if/when you want to dig in again, I can reopen.

Also if anyone else has similar issues and can figure out reproduction steps, feel free to post here. Sorry it isn't working!

I kill the night to unite js build process

now I have all entries in 1 webpack.config.file,

I set "import/resolver": "webpack" to settings,

and I start to receive same import errors in build process.

after that I add "import/resolver": "babel-eslint" to "settings",

"settings": {
    "ecmascript": 6,
    "import/parser": "webpack",
    "import/parser": "babel-eslint"
  },

and I stop to receiving errors in build process, but still have it in
sublime

2015-12-07 23:57 GMT+08:00 Ben Mosher [email protected]:

Can't blame you. [image: :sweat_smile:] I will close for now, let me know
if/when you want to dig in again, I can reopen.

Also if anyone else has similar issues and can figure out reproduction
steps, feel free to post here. Sorry it isn't working!

โ€”
Reply to this email directly or view it on GitHub
https://github.com/benmosher/eslint-plugin-import/issues/139#issuecomment-162566106
.

I am also seeing this issue, only when using SublimeLinter-eslint.

// /foo.js
import baz from './bar/baz';
// /bar/baz.js
import x from './x';

The error only appears inside nested files, i.e. the import for ./x in /bar/baz.js above. /foo.js has no errors.

@OliverJAsh: would you be up for building a toy example I can debug against? I've been unable to replicate on my end thus far.

I've seen that behavior before, but the SublimeLinter-eslint workaround has been working for me, and hopefully with my next publish of the Node resolver, the workaround is not needed anymore.

Unless maybe it's an rc_search_limit problem?

well, I have my import/export organized like this:

import { func1, func2 } from "./dir/file";
import { func3 } from "./anotherdir/file2";

const func4 = (func1, func2, func3) {...};

export { func4 };

2015-12-09 4:58 GMT+08:00 Ben Mosher [email protected]:

@OliverJAsh https://github.com/OliverJAsh: would you be up for building
a toy example I can debug against? I've been unable to replicate on my end
thus far.

โ€”
Reply to this email directly or view it on GitHub
https://github.com/benmosher/eslint-plugin-import/issues/139#issuecomment-163015751
.

Update: I think I found a cleaner workaround for the SublimeLinter-eslint integration; see the updated README about the chdir setting, and let me know if it fixes this for you?

Ben, I create project file in sublime and make it

{
"folders":
[
{
"path": "."
}
],
"SublimeLinter":
{
"linters":
{
"eslint":
{
"chdir": "${project}/."
}
}
}
}

it doesn't help (

2015-12-14 19:56 GMT+08:00 Ben Mosher [email protected]:

Update: I think I found a cleaner workaround for the SublimeLinter-eslint
integration; see the updated README
https://github.com/benmosher/eslint-plugin-import#sublimelinter-eslint
about the chdir setting, and let me know if it fixes this for you?

โ€”
Reply to this email directly or view it on GitHub
https://github.com/benmosher/eslint-plugin-import/issues/139#issuecomment-164419292
.

Dang.

Could try just "chdir": "${project}" but I suspect, given all the things that already haven't, that it also won't work.

What OS are you on? I just realized I can't tell from the stuff you've provided so far, I guess I had assumed OS X. If you're on Windows, could be something nutty related to that? (though even then, I wouldn't know where to start, I don't have access to a Windows box)

I'm on Mac, last version updated, Node 5.1.0

2015-12-14 23:29 GMT+08:00 Ben Mosher [email protected]:

Dang.

Could try just "chdir": "${project}" but I suspect, given all the things
that already haven't, that it also won't work.

What OS are you on? I just realized I can't tell from the stuff you've
provided so far, I guess I had assumed OS X. If you're on Windows, could be
something nutty related to that? (though even then, I wouldn't know where
to start, I don't have access to a Windows box)

โ€”
Reply to this email directly or view it on GitHub
https://github.com/benmosher/eslint-plugin-import/issues/139#issuecomment-164467293
.

Sublime text 3

2015-12-15 1:35 GMT+08:00 Alexey Lyahov [email protected]:

I'm on Mac, last version updated, Node 5.1.0

2015-12-14 23:29 GMT+08:00 Ben Mosher [email protected]:

Dang.

Could try just "chdir": "${project}" but I suspect, given all the things
that already haven't, that it also won't work.

What OS are you on? I just realized I can't tell from the stuff you've
provided so far, I guess I had assumed OS X. If you're on Windows, could be
something nutty related to that? (though even then, I wouldn't know where
to start, I don't have access to a Windows box)

โ€”
Reply to this email directly or view it on GitHub
https://github.com/benmosher/eslint-plugin-import/issues/139#issuecomment-164467293
.

BTW, I fount that if I remove "import/parser": "babel-eslint" from
"settings": {
"ecmascript": 6,
"import/parser": "webpack",
"import/parser": "babel-eslint"
},
in .eslint.rc, I have "import/default" rule error

2015-12-15 1:35 GMT+08:00 Alexey Lyahov [email protected]:

Sublime text 3

2015-12-15 1:35 GMT+08:00 Alexey Lyahov [email protected]:

I'm on Mac, last version updated, Node 5.1.0

2015-12-14 23:29 GMT+08:00 Ben Mosher [email protected]:

Dang.

Could try just "chdir": "${project}" but I suspect, given all the
things that already haven't, that it also won't work.

What OS are you on? I just realized I can't tell from the stuff you've
provided so far, I guess I had assumed OS X. If you're on Windows, could be
something nutty related to that? (though even then, I wouldn't know where
to start, I don't have access to a Windows box)

โ€”
Reply to this email directly or view it on GitHub
https://github.com/benmosher/eslint-plugin-import/issues/139#issuecomment-164467293
.

I'm curious if you still have this issue as of ESLint 2.x + the 1.0.0+ version of this plugin.

Closing due to inactivity, just let me know if you want to pick this back up if it's still not working.

Stil having the issue, sublime 3 latest eslint latest plugin version

The workaround that worked for me was:

Preferences > Packages settings > SublimeLinter > Settings [User]

Then add "args": ["--stdin-filename", "@"] to the linters.eslint entry

Still doesn't work for me either(

2016-03-09 20:50 GMT+07:00 Vincent Voyer [email protected]:

The workaround that worked for me was:

Preferences > Packages settings > SublimeLinter > Settings [User]

Then add "args": ["--stdin-filename", "@"] to the linters.eslint entry

โ€”
Reply to this email directly or view it on GitHub
https://github.com/benmosher/eslint-plugin-import/issues/139#issuecomment-194304054
.

I think I might petition @roadhump to remove the .eslintignore behavior from the Sublime plugin now that ESLint 2 is released. IIRC it should be unnecessary now.

I had a different problem with the same error.
We use extension-less imports but point to more types of files (.es6, .coffee, etc).

I added the following line to my eslint.json and got it to work:

  "settings": {
    "import/resolver": {"node": {"extensions": [".es6", ".coffee"]}}
  }

Thanks, @emorikawa! Had the same error for imports of .jsx files without specifying the extension.

Adding the following settings helped:
"settings": { "import/resolver": {"node": {"extensions": [".js", ".jsx"]}} }

Before that tried different settings, including "settings": {"import/extensions": [".js", ".jsx"] } which didn't help.

Using version 1.6.1.

@mikatuo's solution solved it for me - needed .jsx support. I feel like this should be mentioned in the README alongside the note to use the import/extensions setting?

I have tried everything in the README, as well as every single proposed solution in this issue.

Setup

My installed versions:

{
"eslint-import-resolver-webpack": "^0.8.0",
"eslint-plugin-import": "^2.2.0"
}

My problem

I am getting the following linting errors ONLY on Sublime 3. If I run eslint from the console, I do not get these errors at all:

SublimeLinter: eslint: app.js ['/Users/liadrian/Dev/robinapp-web/node_modules/.bin/eslint', '--format', 'compact', '--stdin', '--stdin-filename', '@', '--stdin-filename', '@'] 
SublimeLinter: eslint output:
/Users/liadrian/Dev/robinapp-web/src/@: line 7, col 1, Error - 'setup' should be listed in the project's dependencies. Run 'npm i -S setup' to add it (import/no-extraneous-dependencies)
/Users/liadrian/Dev/robinapp-web/src/@: line 7, col 23, Error - Unable to resolve path to module 'setup/store'. (import/no-unresolved)
/Users/liadrian/Dev/robinapp-web/src/@: line 7, col 23, Error - Missing file extension for "setup/store" (import/extensions)

4 problems

The code

From {project_root}/src/app.js, I am trying to import {project_root}/src/setup/store.js, like this:

import initStore from 'setup/store';

Note again that this works in every instance, just not on Sublime 3 for some reason.

Config files

My Webpack config uses ./src for root resolution, so the app compiles fine:

// ...
resolve: {
  root: path.resolve('./src'),
  extensions: ['', '.js'],
},
// ...

I put in a .sublime-project file:

{
    "folders":
    [
        {
            "path": "src"
        }
    ],
    "SublimeLinter":
    {
        "linters":
        {
            "eslint":
            {
                "chdir": "${project}/src"
            }
        }
    }
}

In my SublimeLinter.sublime-settings (aka Preferences > Package Settings > SublimeLinter > Settings - User):

"linters": {
    "eslint": {
        "@disable": false,
        "args": [
            "--stdin-filename",
            "@"
        ],
        "excludes": []
    }
},
"rc_search_limit": null,

And finally, my .eslintrc file:

{
  "ecmaFeatures": {
    "jsx": true,
    "modules": true
  },
  "env": {
    "browser": true,
    "es6": true,
    "node": true
  },
  "parser": "babel-eslint",
  "extends": "eslint-config-airbnb",
  "rules": {
    "react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
  },
  "plugins": [
    "react"
  ],
  "settings": {
    "import/extensions": [".js", ".jsx"],
    "import/parser": "babel-eslint",
    "import/resolver": {
      "node": {
        "extensions": [".js", ".jsx"]
      },
      "webpack": {
        "config": "webpack.dev.config.js"
      }
    }
  }
}

What am I missing?

I just want to add that I got it working when I realized that there is a difference between global eslint file and local (in the project) eslint file. Also you need to install eslint-import-resolver-webpack.

@adrianmcli no obvious problems (it's been forever since I've debugged this, though) but you could try an absolute path to your webpack.dev.config.js. Easiest way to to spec your .eslintrc as .eslintrc.js and use path.join(__dirname, "[intermediate path]", "webpack.dev.config.js"). It's possible that some or all of your files are getting tripped up locating your Webpack config.

@adrianmcli did you figure out a fix?

@benmosher thanks for the suggestion. I did as you suggested and validated that the config was found by adding in a console.log within the config file:

console.log('config found');

Inside the Sublime console, I get this:

SublimeLinter: eslint: app.js ['/Users/liadrian/Dev/robinapp-web/node_modules/.bin/eslint', '--format', 'compact', '--stdin', '--stdin-filename', '@', '--stdin-filename', '@'] 
SublimeLinter: eslint output:
config found
/Users/liadrian/Dev/robinapp-web/src/@: line 7, col 1, Error - 'setup' should be listed in the project's dependencies. Run 'npm i -S setup' to add it (import/no-extraneous-dependencies)
/Users/liadrian/Dev/robinapp-web/src/@: line 7, col 23, Error - Unable to resolve path to module 'setup/store'. (import/no-unresolved)
/Users/liadrian/Dev/robinapp-web/src/@: line 7, col 23, Error - Missing file extension for "setup/store" (import/extensions)

4 problems

So at least we know that the config file is being found properly. What else can I check for?

@mahdt as you can see, I'm still struggling with this issue. I used a lot of eslint-disable-lines to get through this in the mean time :(

@adrianmcli I just figured out the issue I had.
My projectRoot/.eslintrc file

"settings": {
  "import/resolver": {
    "webpack": {
      "config": "./webpack/webpack.eslint.js"
    }
  }
}

Originally in my projectRoot/webpack/webpack.eslint.js file, I had

const path = require('path');

module.exports = {
  context: process.cwd(),
  resolve: {
    modules: [
      path.resolve('./src/')
    ]
  }
};

Even though this works with Atom, it does not with Sublime.

I realized that Sublime was changing the context (process.cwd()) to the directory of the open file. Thus eslint was unable to find the local modules.
So I changed my projectRoot/webpack/webpack.eslint.js file:

const path = require('path');

module.exports = {
  resolve: {
    modules: [
      path.resolve(__dirname, '..', 'src')
    ]
  }
};

So I was able to fix the problem by providing the full path to my src directory.

@mahdt BRILLIANT! This worked. Thanks so much for digging into this.

@mahdt worked for me too! ๐Ÿ˜€ I was trying to use eslint-plugin-import and eslint-import-resolver-webpack with Create React App, and a call to process.cwd() was the culprit. I ended up changing the paths.js file to refer explicitly to the app root instead of using process.cwd().

using

import/resolver:
  node:
    extensions:
    - '.js'
    - '.jsx'

worked for me. Should add this to the README

@mahdt's solution worked for me - though I was having the issues in vscode, not sublime.

However, I needed a slight modification to correctly resolve imports from node_modules. I also realised the webpack config can be defined inline in .eslintrc.js, meaning i could simplify the modules paths.

So here was my final solution:

// .eslintrc.js
const path = require("path");

const config = {
  resolve: {
    modules: [
      path.resolve(__dirname, "src"),
      path.resolve(__dirname, "node_modules")
    ]
  }
};

module.exports = {
  settings: {
    "import/resolver": {
      webpack: {
        config
      }
    }
  }
};
Was this page helpful?
0 / 5 - 0 ratings