How to make eslint import/first understand my webpack aliases?

Viewed 6773

I'm using eslint-import-resolver-webpack to make my eslint aware of my webpack aliases.

However I still have one issue left:
eslint is reporting: Absolute imports should come before relative imports. eslint(import/first)

How can I make eslint understand that my aliased imports are relative imports not absolute?


Here is more info about my setup:

| source
 - | app
    - | containers
       - | CalendarContainer.js
| .eslintrc
| webpack.config.dev.js
| webpack.config.resolve.js

webpack.config.dev.js

const resolveConfig = require('./webpack.config.resolve')
const appConfig = {
  // lots of config,
  ...resolveConfig // to have the resolve key with aliases underneath
}
module.exports = [appConfig, ...]

webpack.config.resolve.js

const path = require('path');

module.exports = {
    resolve: {
        extensions: ['.js'],
        modules: ['node_modules'],
        alias: {
            '~containers': path.resolve(__dirname, 'source/app/containers'),
            // ...
        },
    },
};

.eslintrc

{  
  // ...
  "settings": {
    "import/resolver": {
      "webpack": {
        "config": "webpack.config.resolve.js"
      }
    }
  },
  // ...
}

CalendarContainer.js

import React, { useEffect, useState } from 'react';

import Button from '../Components/Button';
import SearchModalContainer from '~containers/SearchModalContainer';

This works all fine except for the line with:
import SearchModalContainer from '~containers/SearchModalContainer';

Indeed, eslint still report an error with:
Absolute imports should come before relative imports.eslint(import/first)

How can I make eslint understand that this import is a relative import not an absolute one?

(and I don't want to disable this rule on the eslint configuration)

1 Answers

Use eslint-import-resolver-alias

Just replace eslint-import-resolver-webpack with eslint-import-resolver-alias and you'll get easy to configure aliases for eslint.

Related