Skip to content

Fix the error when using prettier-eslint extension in VS Code with import/order rule and typescript alias #18

@AmBeta

Description

@AmBeta

Problem

When enable the import/order rule in eslint like following:

modules.exports = {
  rules: {
    'import/order': [
      'error',
      {
        groups: [
          'type',
          'builtin',
          'external',
          'internal',
          'parent',
          'sibling',
          'index',
          'object',
        ],
      },
    ],
  }
}

Format file with prettier-eslint plugin fails to correctly order the import statements, and gives this error below:

Unable to resolve path to module '@/utils.ts'

Where @ is some path alias defined in the project's tsconfig.json file.

Reason

After debugging line by line of eslint, I finaly got that it is the eslint-import-resolver-typescript plugin which cannot parse the import path with an alias.
The plugin is set default to resolve the project config file with path '/tsconfig.json'. While running outside of the project by the VS Code extension, the <root> path is not correctly defined, so the project's config file is not resolvable and the path aliases are not loaded.

Solution

Just specify the absolute project config file path in your project's eslint config file.

const path = require('path');

modules.exports = {
  settings: {
    'import/resolver': {
      typescript: {
        project: path.resolve(__dirname, './tsconfig.json'),
      },
    },
  },
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions