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
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.
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.
# 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.
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.
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": {
"@": "./"
}
}
]
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With