Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AmBeta opened this issue Jan 12, 2024 · 0 comments
Labels

Comments

@AmBeta
Copy link
Owner

AmBeta commented Jan 12, 2024

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'),
      },
    },
  },
}
@AmBeta AmBeta added the VS Code label Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant