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

import/no-unresolved with webpack modules/aliases in the VS Code #464

Closed
ValeryVS opened this issue May 7, 2018 · 17 comments
Closed

import/no-unresolved with webpack modules/aliases in the VS Code #464

ValeryVS opened this issue May 7, 2018 · 17 comments
Labels
feature-request Request for new features or functionality

Comments

@ValeryVS
Copy link

ValeryVS commented May 7, 2018

Update:

So, there are few separate issues.

  1. To fix issue now, I can adjust my config: replace object spreading with Object.assign, or use babel-register.
  2. Something interpret Unexpected token ... as ESLint error and show no-undesolved, no-duplicate and other.
    2018-05-10 19 55 21
    Which is wrong - it should be SyntaxError in ESLint output.
    index js__marketnode-2018-05-10-19-53-42-osox6
    It happens because node v7.9.0 in VS Code can't launch webpack config with object spread syntax.
  3. May be vscode-eslint can launch .eslintrc.js and webpack config with local node, not with bundles one?

Original text

Faced with such issue.
There is no errors while launching eslint from terminal.

2018-05-07 15 39 51

2018-05-07 15 40 02

Trying to reproduce error in clean repository, but without success yet.

@ValeryVS
Copy link
Author

ValeryVS commented May 7, 2018

I can reproduce the similar error, with Type Error in webpack configuration. (when webpack.config.js can not be launched).
But in my original project webpack config works.

@alecf
Copy link

alecf commented May 7, 2018

This was driving me bonkers and I was getting other odd errors from the import plugin. Just for kicks I put:

require('babel-register')

at the top of my .eslintrc and magically everything seems to work now! (I was already using babel-register elsewhere in my project, so it was already installed)

It seems like vscode-eslint runs eslint as a library, and JS imports aren't recognized without babel-register

@dbaeumer
Copy link
Member

dbaeumer commented May 8, 2018

@ValeryVS how to you launch eslint from the terminal.

I think this is related to #455 (comment)

@alecf adding require('babel-register') is a nice fix to solve the issue.

@dbaeumer dbaeumer added the info-needed Issue requires more information from poster label May 8, 2018
@ValeryVS
Copy link
Author

@alecf
@dbaeumer
No, my webpack config doesn't require babel. It use require() for imports, etc.

But, when I create some simple webpack configuration - it works.
The reason is definitely in webpack config, but it is something else, not the new syntax.
I will try to debug it this week.

@ValeryVS
Copy link
Author

ValeryVS commented May 10, 2018

@dbaeumer
Found it!

Our project is bundled with node 8.
So, there is some object destructions in webpack configuration.

But there is node v7.9.0 in VS Code! Webpack config executed with node 7.9.0 when vscode-eslint try to execute .eslintrc.js with eslint-plugin-import and eslint-import-resolver-webpack.

The wierd thing is, that in most of my files I have strange errors, like at the first image in that issue.
[eslint] Resolve error: Unexpected token ... (import/no-unresolved)
[eslint] Resolve error: Unexpected token ... (import/no-duplicates)
screen
And ESLint output in VS Code was pretty clean
screen-2018-05-10-19-43-03-wx1p2

But, when I add one more resolver in my .eslintrc.js

'node': {
    'paths': [
        __dirname,
    ],
},

resolve errors are gone, and I, finally, saw the real error

[Info  - 19:40:18] ESLint server is running.
[Info  - 19:40:19] ESLint library loaded from: /Users/tasogare/Projects/MY_FOLDER/node_modules/eslint/lib/api.js
[Error - 19:40:20] ESLint stack trace:
[Error - 19:40:20] /Users/tasogare/Projects/MY_FOLDER/configs/webpack/browser.js:46
            ...grab(OLD_STYLE_PAGES_DIR, 'browser.js'),
            ^^^
SyntaxError: Unexpected token ...
    at createScript (vm.js:53:10)
    at Object.runInThisContext (vm.js:95:10)
    at Module._compile (module.js:543:28)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/tasogare/Projects/MY_FOLDER/configs/webpack/development.js:9:23)

@ValeryVS
Copy link
Author

So, there are few separate issues.

  1. I can adjust my config: replace object spreading with Object.assign.
  2. Something interpret Unexpected token ... as ESLint error and show no-undesolved, no-duplicate and other.
    2018-05-10 19 55 21
    Which is wrong - it should be SyntaxError in ESLint output.
    index js__marketnode-2018-05-10-19-53-42-osox6
  3. May be vscode-eslint can launch .eslintrc.js and webpack config with local node, not with bundles one?

@ValeryVS
Copy link
Author

1.1
Ok, now I'm understand how to solve this with babel-register

require('babel-register')({
    presets: [ 'es2015', 'stage-2' ],
});

It wasn't help me before, because, with default settings, it can't handle spread operator.
With stage-2 it can.

@dbaeumer
Copy link
Member

@ValeryVS thanks for the great analysis. Very helpful.

I will keep the item open to look into 2 and 3

@dbaeumer dbaeumer added feature-request Request for new features or functionality and removed info-needed Issue requires more information from poster labels May 11, 2018
@dbaeumer dbaeumer added this to the On Deck milestone May 11, 2018
@viT-1
Copy link

viT-1 commented Feb 14, 2019

#405 (comment)

Still have this issue with VSCode 1.30.1
Project is bundled normally, but eslint plugin underlines me "incorrect" imports in paths with webpack aliases.

Here I found workaround but don't understand how to fix .eslintrc file.
#464 (comment)

No solution to the problem found still?

@viT-1
Copy link

viT-1 commented Feb 14, 2019

@alecf Can you show your root .eslintrc workaround?
Should I install babel-register globally?

@viT-1
Copy link

viT-1 commented Feb 14, 2019

I changed webpack config from this:
resolve: { alias: { '~': path.resolve(__dirname), '@': path.resolve(__dirname, './src'), }, },
to this:
resolve: { alias: { '~': path.resolve(__dirname), '@': path.resolve(__dirname, 'src'), }, },

It seems to be ok. But intellisence not showing fresh added methods in imported file,
but show arguments intellisence after I writing the name of "fresh" method completely.

@callumlocke
Copy link

Thanks @alecf for the workaround. Switching from .eslintrc to .eslintrc.js and adding require('@babel/register') fixed it for me.

Still, I don't think this hack should be necessary, seems like this is a bug in the VSCode plugin, if the CLI works fine without the hack.

By the way, this only started happening to me after upgrading to Babel 7 and switching from .babelrc to babel.config.js, not sure if that's relevant.

@dbaeumer
Copy link
Member

Regarding #464 (comment)

Can someone provide me with a GitHub repository that I can clone with steps on how to reproduce this?

Regarding using a different node version than the one that is shipped in VS Code. Use the eslint.runtime setting

@shapkarin
Copy link

Maybe this extention should parse jsconfig.json? Users need that file to make IDE understand aliases.

@dbaeumer
Copy link
Member

I think if this is necessary then eslint itself or the corresponding eslint plugin should read the jsconfig.json file.

@shapkarin
Copy link

shapkarin commented Aug 29, 2019

Seems that it's eslint-plugin-import problem
import-js/eslint-plugin-import#496
And probably I can find some solution in eslint-plugin-import issue.

@dbaeumer
Copy link
Member

dbaeumer commented Oct 1, 2021

I will close the issue. There is now a setting eslint.runtime which lets you point to a newer/different runtime than the one we ship in VS Code.

@dbaeumer dbaeumer closed this as completed Oct 1, 2021
@dbaeumer dbaeumer removed this from the Backlog milestone Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests

6 participants