Vscode-eslint: import/resolver not working for webpack aliases in VSCode eslint

Created on 18 Oct 2019  路  3Comments  路  Source: microsoft/vscode-eslint

Hi, I'm using the eslint-plugin-import along with eslint in a project which as this folder structure:

project_root/
|_ server/
  |_ src/
  |_ .eslintrc
|_ client/
  |_ src/
  |_ .eslintrc
  |_ tools
    |_ webpack.config.js

I have defined some aliases in webpack like so:

/* client/webpack.config.js */
config = {
  resolve: {
    alias: {
        Common: path.resolve('src/common'),
        IP: path.resolve('src/apps/inv-planner'),
    },
    modules: ['node_modules', 'src'],
    extensions: ['.js'],
  },
}

This is my relevant eslint config:

    settings: {
        'import/resolver': {
            node: {
                moduleDirectory: ['node_modules', 'src'],
            },
            webpack: {
                config: `${__dirname}/tools/webpack.config.js`, // vscode doesn't resolve this
                // config: `./tools/webpack.config.js`, // vscode doesn't resolve this either
            },
        },
    },

I have opened the project_root folder in vscode, and all eslint rules work fine in VSCode, except for import/extensions, which keeps throwing errors for imports from the src/apps/inv-planner directory, but not from the src/common directory.

import AsyncRequests from 'IP/components/AsyncRequests'; // throws error
import Instrumentation from 'Common/extensions/instrumentation'; // doesn't throw

The eslint CLI works fine, but the vscode extension doesn't. Is there an issue in path resolution for non-root directories in this plugin?

needs more info

Most helpful comment

I am pretty sure that the eslint.workingDirectories setting is your friend. You even might to set "changeProcessCWD": true. Something like:

 "eslint.workingDirectories": [
   { "directory": "./client", "changeProcessCWD": true },
   { "directory": "./server", "changeProcessCWD": true }
 ]

If this doesn't help please provide me with a GitHub repository I can clone that demos what you are seeing.

All 3 comments

I am pretty sure that the eslint.workingDirectories setting is your friend. You even might to set "changeProcessCWD": true. Something like:

 "eslint.workingDirectories": [
   { "directory": "./client", "changeProcessCWD": true },
   { "directory": "./server", "changeProcessCWD": true }
 ]

If this doesn't help please provide me with a GitHub repository I can clone that demos what you are seeing.

Thanks, I'll try it out and report back next week

It works. Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bacsonvv picture bacsonvv  路  6Comments

lstrzebinczyk picture lstrzebinczyk  路  6Comments

greggman picture greggman  路  7Comments

lednhatkhanh picture lednhatkhanh  路  4Comments

julian-sb1 picture julian-sb1  路  6Comments