[import/no-unresolved] when using with typescript and "paths" option

See original GitHub issue

I get [import/no-unresolved] error with eslint while using typescript tsconfig.json paths option.

nextjs + typescript + eslint

My eslint file

module.exports = {
    root: true,
    parser: '@typescript-eslint/parser',
    parserOptions: {
        ecmaFeatures: { jsx: true },
    },
    extends: [
        'eslint:recommended',
        'plugin:@typescript-eslint/eslint-recommended',
        'plugin:@typescript-eslint/recommended',
        'plugin:react/recommended',
        'plugin:jsx-a11y/recommended',
        'plugin:import/errors',
        'plugin:import/warnings',
        'plugin:import/typescript',

        // Prettier plugin and recommended rules
        'plugin:prettier/recommended',
    ],
    rules: {
        // Include .prettierrc.js rules
        'prettier/prettier': ['error', {}, { usePrettierrc: true }],

        'no-empty': 'off',
        'react/prop-types': 'off',
        '@typescript-eslint/ban-ts-comment': 'off',
        '@typescript-eslint/no-non-null-assertion': 'off',
        'jsx-a11y/click-events-have-key-events': 'off',
        'jsx-a11y/no-static-element-interactions': 'off',
        'jsx-a11y/interactive-supports-focus': 'off',
        'jsx-a11y/no-noninteractive-element-interactions': 'off',
        'jsx-a11y/no-onchange': 'off',
    },
    settings: {
        'import/extensions': ['.js', '.jsx', '.tsx', '.ts'],
        'import/resolver': {
            typescript: {},
        },
    },
};

In tsconfig.json i use paths

"paths": {
      "@/core/*": ["src/core/*"],
}

and get errors from eslint like

  18:23  error  Unable to resolve path to module '@/core/useRouter'                  import/no-unresolved

Compilation build all works great, just linting fails eslint

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:13
  • Comments:17 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
loweojcommented, Apr 22, 2022

On the plugin docs (https://github.com/alexgorbatchev/eslint-import-resolver-typescript):

Adding

  settings: {
    "import/parsers": {
      "@typescript-eslint/parser": [".ts", ".tsx"],
    },
    "import/resolver": {
      typescript: {},
    },
  },

has worked for me - the import/parsers bit seemed to fix it.

this is with "eslint-import-resolver-typescript": "^2.7.1",

1reaction
damiangreencommented, Nov 11, 2022

I’ve tried all the suggestions above and still get this error. Can anyone help

// ESLint configuration
// http://eslint.org/docs/user-guide/configuring
// eslint-disable-next-line
const path = require('path')

module.exports = {
  parser: '@typescript-eslint/parser',
  parserOptions: {
    warnOnUnsupportedTypeScriptVersion: false,
  },
  extends: [
    'airbnb',
    'plugin:@typescript-eslint/recommended',
    'plugin:react/recommended',
    'plugin:css-modules/recommended',
    'prettier',
    'plugin:storybook/recommended',
    //'plugin:unicorn/recommended',
  ],
  plugins: [
    'css-modules',
    'prettier',
    'graphql',
    '@typescript-eslint',
    'formatjs',
    'react-hooks',
    'unused-imports',
    'testing-library',
    'unicorn',
  ],
  overrides: [
    {
      // 3) Now we enable eslint-plugin-testing-library rules or preset only for matching files!
      files: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
      extends: ['plugin:testing-library/react'],
      rules: {
        'testing-library/no-unnecessary-act': 'warn',
        'testing-library/prefer-find-by': 'warn',
        'testing-library/prefer-screen-queries': 'warn',
        'testing-library/await-async-query': 'warn',
        'testing-library/render-result-naming-convention': 'warn',
        'testing-library/no-node-access': 'off',
        // re-enable in future when less noisy
        'testing-library/no-container': 'off',
        // re-enable in future when less noisy
        'testing-library/no-wait-for-side-effects': 'warn',
        'testing-library/no-render-in-setup': 'warn',
        'testing-library/no-wait-for-multiple-assertions': 'warn',
        'testing-library/no-wait-for-snapshot': 'warn',
      },
    },
    {
      files: ['*.ts', '*.tsx', '*.mjs'],
      parserOptions: {
        project: ['./tsconfig.json', './packages/rmm-helpers/tsconfig.json'],
      },
    },
  ],
  globals: {
    __DEV__: true,
    __MAINT__: true,
  },
  env: {
    browser: true,
    jest: true,
  },
  rules: {
...
    'import/extensions': 'off',
    // TODO: Figure out why this started breaking
    'import/order': [
      'error',
      {
        'newlines-between': 'never',
        alphabetize: {
          order: 'asc',
          caseInsensitive: true,
        },
        pathGroups: [
          {
            pattern: 'react',
            group: 'external',
            position: 'before',
          },
          {
            pattern: '*.css',
            group: 'unknown',
            patternOptions: {
              matchBase: true,
            },
            position: 'after',
          },
        ],
        pathGroupsExcludedImportTypes: ['react'],
      },
    ],
    'unused-imports/no-unused-imports-ts': 2,
    'no-unused-expressions': 'off',
    '@typescript-eslint/no-unused-expressions': 'error',
    'prefer-template': 2,
    'graphql/template-strings': [
      'error',
      {
        // Import default settings for your GraphQL client. Supported values:
        // 'apollo', 'relay', 'lokka', 'fraql', 'literal'
        // env: 'literal',
        // Import your schema JSON here
        // schemaJson: require('./src/generated/schema.json'), // TODO: do we need this line?>
        // OR provide absolute path to your schema JSON (but not if using `eslint --cache`!)
        schemaJsonFilepath: path.resolve(
          __dirname,
          './src/generated/schema.json',
        ),
        // OR provide the schema in the Schema Language format
        // schemaString: printSchema(schema),
        tagName: 'graphql', // tagName is gql by default
      },
    ],
    // https://github.com/formatjs/formatjs/issues/785
    // react-intl some components use 'style' prop
    'react/style-prop-object': [
      'error',
      {
        allow: [
          'FormattedNumber',
          'FormattedDateParts',
          'FormattedRelativeTime',
        ],
      },
    ],
  },
  settings: {
    'import/parsers': {
      '@typescript-eslint/parser': ['.ts', '.tsx'],
    },
    'import/extensions': ['.js', '.jsx', '.tsx', '.ts'],
    // Allow absolute paths in imports, e.g. import Button from 'components/Button'
    // https://github.com/benmosher/eslint-plugin-import/tree/master/resolvers
    'import/resolver': {
      typescript: {}, // this loads <rootdir>/tsconfig.json to eslint
      node: {
        moduleDirectory: ['node_modules', 'src', 'packages/rmm-helpers/src'],
        extensions: ['.js', '.tsx', '.ts'],
      },
    },
    'css-modules': {
      basePath: 'src',
    },
  },
}

my root tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./src",
    "paths": {
      "rmm-helpers": ["../packages/rmm-helpers/lib"]
     },
    "allowJs": true,
    "outDir": "./lib",
    "allowSyntheticDefaultImports": true,
    "lib": ["es5", "es6", "es7", "es2020", "es2021", "dom"],
    "sourceMap": true,
    "noEmitHelpers": false, // this need true for unit test
    "noImplicitAny": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "esModuleInterop": true,
    "module": "esnext",
    "resolveJsonModule": true,
    "target": "es6",
    "downlevelIteration": true,
    "moduleResolution": "node",
    "jsx": "react-jsx",
    "skipLibCheck": true,
    "noErrorTruncation": true,
    "assumeChangesOnlyAffectDirectDependencies": true,
    // "strictNullChecks": true
    //"strict": true // Enable soon
    "plugins": [
      {
        "name": "typescript-plugin-css-modules",
        "options": {
          "customMatcher": "\\.css$",
        }
      }
    ]
  },
  "include": ["./src/**/*", "./tools/**/*", "./.storybook/*", "./packages/rmm-helpers/**/*"], //, "./packages/rmm-generated/*"
    "references": [
      { "path": "./packages/rmm-helpers" },
    ],
}

My referenced typescript project tsconfig.json

{
  "extends": "../../tsconfig.settings.json",
  "include": ["./src/**/*"],
  "compilerOptions": {
    "outDir": "./lib",
    "rootDir": "./src"
  },
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

[import/no-unresolved] when using with typescript "baseUrl ...
[import/no-unresolved] when using with typescript "baseUrl" and "paths" ... It should respect baseUrl and paths option on tsconfig.json.
Read more >
Using eslint with typescript - Unable to resolve path ...
In my eslintrc.js config file, the "import/resolver" object needed to sit within the "rules" node, not the " ...
Read more >
eslint-import-resolver-typescript
Start using eslint-import-resolver-typescript in your project by running `npm i eslint-import-resolver-typescript`.
Read more >
Typescript — How to solve the problem with unresolved ...
Since the outDir is the “build” subfolder, we have to check one by one all the .js files under it, to find if...
Read more >
Typescript – How to solve the problem with unresolved path ...
js file containing the line with the path alias. The actual cause of the issue is that the tsc compiler does not transpiles...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found