Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to resolve path to module (import/no-resolved) only in Sublime text 3 #139

Closed
JustFly1984 opened this issue Dec 4, 2015 · 41 comments

Comments

@JustFly1984
Copy link

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
    }
}
@benmosher
Copy link
Member

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?

@benmosher
Copy link
Member

@JustFly1984
Copy link
Author

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 notifications@github.com:

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
#139 (comment)
.

@benmosher
Copy link
Member

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.

@benmosher
Copy link
Member

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.

@JustFly1984
Copy link
Author

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 notifications@github.com:

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
#139 (comment)
.

@benmosher
Copy link
Member

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

@JustFly1984
Copy link
Author

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 notifications@github.com:

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
#139 (comment)
.

@benmosher
Copy link
Member

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

@benmosher
Copy link
Member

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.

@JustFly1984
Copy link
Author

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 notifications@github.com:

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
#139 (comment)
.

@benmosher
Copy link
Member

Can't blame you. 😅 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!

@JustFly1984
Copy link
Author

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 notifications@github.com:

Can't blame you. [image: 😅] 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
#139 (comment)
.

@OliverJAsh
Copy link

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.

@benmosher benmosher reopened this Dec 8, 2015
@benmosher
Copy link
Member

@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?

@JustFly1984
Copy link
Author

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 notifications@github.com:

@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
#139 (comment)
.

@benmosher
Copy link
Member

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?

@JustFly1984
Copy link
Author

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 notifications@github.com:

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
#139 (comment)
.

@benmosher
Copy link
Member

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)

@JustFly1984
Copy link
Author

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

2015-12-14 23:29 GMT+08:00 Ben Mosher notifications@github.com:

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
#139 (comment)
.

@JustFly1984
Copy link
Author

Sublime text 3

2015-12-15 1:35 GMT+08:00 Alexey Lyahov justfly1984@gmail.com:

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

2015-12-14 23:29 GMT+08:00 Ben Mosher notifications@github.com:

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
#139 (comment)
.

@JustFly1984
Copy link
Author

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 justfly1984@gmail.com:

Sublime text 3

2015-12-15 1:35 GMT+08:00 Alexey Lyahov justfly1984@gmail.com:

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

2015-12-14 23:29 GMT+08:00 Ben Mosher notifications@github.com:

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
#139 (comment)
.

@benmosher
Copy link
Member

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

@benmosher
Copy link
Member

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

@vvo
Copy link

vvo commented Mar 9, 2016

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

@vvo
Copy link

vvo commented Mar 9, 2016

The workaround that worked for me was:

Preferences > Packages settings > SublimeLinter > Settings [User]

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

@JustFly1984
Copy link
Author

Still doesn't work for me either(

2016-03-09 20:50 GMT+07:00 Vincent Voyer notifications@github.com:

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
#139 (comment)
.

@benmosher
Copy link
Member

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.

@emorikawa
Copy link

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"]}}
  }

@mikatuo
Copy link

mikatuo commented Jun 5, 2016

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.

@sman591
Copy link

sman591 commented Nov 15, 2016

@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?

@adrianmcli
Copy link

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?

@developer239
Copy link

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.

@benmosher
Copy link
Member

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

@mahdt
Copy link

mahdt commented Mar 16, 2017

@adrianmcli did you figure out a fix?

@adrianmcli
Copy link

@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 :(

@mahdt
Copy link

mahdt commented Mar 16, 2017

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

@adrianmcli
Copy link

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

@dpikt
Copy link

dpikt commented Jun 19, 2017

@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().

@jshnaidman
Copy link

using

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

worked for me. Should add this to the README

@WickyNilliams
Copy link

WickyNilliams commented Feb 22, 2018

@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
      }
    }
  }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests