Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make eslint resolve paths mapped in jsconfig

In my nextjs project I have mapped path in jsconfig.json to make easy absolute imports

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@/*": ["./*"]
    },
    "target": "es6",
    "module": "commonjs",
    "experimentalDecorators": true
  }
}

My import paths look like this import { VIEW } from '@/src/shared/constants';

My eslintrc.js has settings specified as

module.exports = {
    ... ,
    settings: {
        "import/resolver": {
          alias: {
            extensions: [".js"],
            map: ["@", "."]
          }
        }
      }
}

I am still getting the error saying can't resolve "@/what/ever/my/path/is"

How do I make eslint realize the jsconfig path

like image 459
mdanishs Avatar asked Jan 23 '20 14:01

mdanishs


People also ask

How do I use configuration files in ESLint?

The first way to use configuration files is via .eslintrc.* and package.json files. ESLint will automatically look for them in the directory of the file to be linted, and in successive parent directories all the way up to the root directory of the filesystem ( / ), the home directory of the current user ( ~/ ), or when root: true is specified.

How to configure ESLint rules in NodeJS?

These rules are being set by us in .eslintrc.json file. This is how you may configure .eslintrc config file in NodeJS. Moreover, you may visit the official eslint site and configure your file with the rules that you want to apply.

What does the presence of jsconfig file in a directory indicate?

# The presence of jsconfig.json file in a directory indicates that the directory is the root of a JavaScript Project. The jsconfig.json file specifies the root files and the options for the features provided by the JavaScript language service.

How does ESLint find the root directory?

If there is a .eslintrc file in the same directory as the file being linted, then that configuration takes precedence. ESLint then searches up the directory structure, merging any .eslintrc files it finds along the way until reaching either a .eslintrc file with root: true or the root directory.


Video Answer


2 Answers

I was using babel-eslint as my parser in eslintrc. While searching, I realized I need to add babel-plugin-module-resolver in babelrc to resolve the modules. In this file we can define our mapped paths which are there in our jsconfig.

Hence adding the following plugin in the babelrc file compiled my code successfully.

[
    "module-resolver",
    {
        "alias": {
          "@": "./"
        }
    }
]
like image 122
mdanishs Avatar answered Nov 02 '22 17:11

mdanishs


According to the docs for eslint-import-resolver-alias, the map property should be an array of arrays, so try this:

module.exports = {
    ... ,
    settings: {
        "import/resolver": {
          alias: {
            extensions: [".js"],
            map: [ ["@", "."] ]
          }
        }
      }
}

Also, double-check that you actually have eslint-import-resolver-alias installed - it's easy to forget!

like image 23
Duncan Thacker Avatar answered Nov 02 '22 17:11

Duncan Thacker