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

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

Closed
jjangga0214 opened this issue Sep 20, 2019 · 20 comments

Comments

@jjangga0214
Copy link

jjangga0214 commented Sep 20, 2019

Repro

tree
<root>
├── src
│   ├── index.ts
│   └── foo.ts
├── tsconfig.json
├── .eslintrc.js
...
└── package.json
tsconfig.json

Other options are omiited for brevity.

{
  "compilerOptions": {
    "baseUrl": "./src"
    "paths": {
      "#/*": ["./*"]
    }
  }
}
.eslintrc.js

Other options are omiited for brevity.

module.exports = {
  parser: '@typescript-eslint/parser',
  root: true,
  env: {
    node: true,
    es6: true,
  },
  plugins: ['@typescript-eslint', ],
  extends: [
    'plugin:@typescript-eslint/recommended',
    'plugin:import/typescript',
  ],
}
src/index.ts
import { foo } from '#/foo'

console.log(`index.ts uses ${foo}`)

Problem

Expected Result

It should respect baseUrl and paths option on tsconfig.json

Actual Result

error  Unable to resolve path to module '#/foo'  import/no-unresolved

Additional Info

https://github.com/jjangga0214/ts-boilerplate/commit/af74c178a3c5ee84f78c520ba3c09ad73a9fc130/checks is the actual result with an error message. (Expand it by clicking "Lint" on github action checks)

Versions

package version
@typescript-eslint/eslint-plugin 2.2.0
@typescript-eslint/parser 2.2.0
eslint-plugin-import 2.18.2
TypeScript 3.6.3
ESLint 5.3.0
node 12.6.0
npm 6.10.2
yarn 1.17.3
@jjangga0214 jjangga0214 changed the title [import/no-unresolved] when using with typescript "baseUrl" and "path" option [import/no-unresolved] when using with typescript "baseUrl" and "paths" option Sep 20, 2019
@ljharb
Copy link
Member

ljharb commented Sep 21, 2019

You'd have to use a typescript resolver for that, and configure it in your eslint settings.

See https://github.com/benmosher/eslint-plugin-import#resolvers and https://github.com/benmosher/eslint-plugin-import/wiki/Resolvers

@ljharb ljharb closed this as completed Sep 21, 2019
@jjangga0214
Copy link
Author

jjangga0214 commented Sep 26, 2019

Thanks!

For other folks, this solves the issue.

npm install eslint-import-resolver-typescript

package.json

{
  // other configuration are omitted for brevity
  settings: {
    "import/resolver": {
      typescript: {} // this loads <rootdir>/tsconfig.json to eslint
    },
  },
}

@alexkaduk
Copy link

alexkaduk commented Jan 7, 2020

@jjangga0214
After eslint-import-resolver-typescript installation

{
  settings: {
    "import/resolver": {
      typescript: {} // this loads <rootdir>/tsconfig.json to eslint
    },
  },
}

should be added to .eslintrc.js.
Thanks.

@Sergioamjr
Copy link

@jjangga0214
After eslint-import-resolver-typescript installation

{
  settings: {
    "import/resolver": {
      typescript: {} // this loads <rootdir>/tsconfig.json to eslint
    },
  },
}

should be added to .eslintrc.js.
Thanks.

I needed to reopen my vscode but this worked very well, thanks

@oarthursilva
Copy link

@jjangga0214 thanks for the suggestion. eslint-import-resolver-typescript worked just fine.

btw, I also added alwaysTryTypes: true, to force the resolve of @types first

cheers

@tpluscode
Copy link

I've already been using typescript resolver but it was necessary to add "project": "packages/*/tsconfig.json" to the resolver's config for it to work

@vortesnail
Copy link

I've already been using typescript resolver but it was necessary to add "project": "packages/*/tsconfig.json" to the resolver's config for it to work

Yes!

For monorepo projects, this is the required configuration. I was confused until I read your comment, thanks!

rafaellehmkuhl added a commit to rafaellehmkuhl/companion-docker that referenced this issue Oct 15, 2021
Supress errors related to `import/no-unresolved` with typescript files (import-js/eslint-plugin-import#1485)
patrickelectric pushed a commit to bluerobotics/BlueOS that referenced this issue Oct 15, 2021
Supress errors related to `import/no-unresolved` with typescript files (import-js/eslint-plugin-import#1485)
@moltenice
Copy link

moltenice commented Oct 22, 2021

@jjangga0214 After eslint-import-resolver-typescript installation

{
  settings: {
    "import/resolver": {
      typescript: {} // this loads <rootdir>/tsconfig.json to eslint
    },
  },
}

should be added to .eslintrc.js. Thanks.

And for .eslintrc.yml:

settings:
  import/resolver:
    typescript: {}

@jneander
Copy link

Is there a configuration, whether with eslint-plugin-import alone or in conjunction with other eslint extensions, which will ensure that the .js extension is present on imports of typescript files? Without another tool appending the extension as needed, it must be done manually to ensure that the tsc-outputted files are valid es modules. Without linter support, I know I will continue to miss extensions where they need to be.

@ljharb
Copy link
Member

ljharb commented Nov 16, 2021

@jneander no, not that i know of. I'd suggest omitting the extension, and using babel instead of tsc as your transpiler (regardless), and then it'd be trivial to add a babel plugin that adds the extensions whenever needed.

DalSoft pushed a commit to DalSoft/create-react-app-webpack-5-module-federation-tailwind-typeScript that referenced this issue Jan 25, 2022
DalSoft pushed a commit to DalSoft/create-react-app-webpack-5-module-federation-tailwind-typeScript that referenced this issue Jan 25, 2022
@SalahAdDin
Copy link

@jjangga0214
After eslint-import-resolver-typescript installation

{
  settings: {
    "import/resolver": {
      typescript: {} // this loads <rootdir>/tsconfig.json to eslint
    },
  },
}

should be added to .eslintrc.js.
Thanks.

I needed to reopen my vscode but this worked very well, thanks

Well, it does not work anymore, hours trying to make it work and I couldn't.

@peter-wd-1
Copy link

peter-wd-1 commented Mar 16, 2022

Dose not work

module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
    "jest/globals": true,
  },
  parser: "@typescript-eslint/parser",
  plugins: ["jest"],
  extends: ["airbnb", "plugin:node/recommended", "plugin:import/typescript"],
  rules: {
    semi: ["error", "never"],
    "node/no-unsupported-features/es-syntax": "off",
  },
  settings: {
    "import/resolver": {
      typescript: {}, // this loads <rootdir>/tsconfig.json to eslint
    },
  },
};

@kristijorgji
Copy link

Hi, it does not work for me using 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

@ljharb
Copy link
Member

ljharb commented Mar 16, 2022

Sounds like a new issue would be appropriate; commenting on a closed one isn’t likely to help anything.

@kristijorgji
Copy link

@ljharb good idea, I created this one you guys can share your samples there
#2405

qqpann added a commit to qqpann/portfolio that referenced this issue Jun 18, 2022
Next.js supports tsconfig path alias out of the box.
Reference: https://nextjs.org/docs/advanced-features/module-path-aliases
Then, resolve eslint error.
Thanks: import-js/eslint-plugin-import#1485
qqpann added a commit to qqpann/nextjs-template that referenced this issue Jun 26, 2022
Next.js supports tsconfig path alias out of the box.
Reference: https://nextjs.org/docs/advanced-features/module-path-aliases
Then, resolve eslint error.
Thanks: import-js/eslint-plugin-import#1485

Removed import/recommended, because it was causing unnecessary error.
@mhfaust
Copy link

mhfaust commented Feb 22, 2024

By the way, by now it seems if you've got a TS app in VSCode with your paths configured correctly in tsconfig.json compilerOptions, you don't really need this rule and you can just disable it. The integrated compiler always catches typos in import paths.

Therefore your best option is just:

rules: [
   "import/no-unresolved": "off",
]

It took me a while to figure that out, and I kicked myself when I did.

@king-koda
Copy link

By the way, by now it seems if you've got a TS app in VSCode with your paths configured correctly in tsconfig.json compilerOptions, you don't really need this rule and you can just disable it. The integrated compiler always catches typos in import paths.

Therefore your best option is just:

rules: [
   "import/no-unresolved": "off",
]

It took me a while to figure that out, and I kicked myself when I did.

thank you bro, I did wonder why I'm spending 5+ hours trying to fix this rubbish when if the pathing was broken TS would of told me, I should of not been so stubborn, set and forget, how miserable HAHA

@SalahAdDin
Copy link

@jjangga0214 if this issue is still happening, it should not be solved.

@jjangga0214
Copy link
Author

jjangga0214 commented Feb 26, 2024

@SalahAdDin What do you mean? Sorry, I don't understand your message.

@SalahAdDin
Copy link

By the way, by now it seems if you've got a TS app in VSCode with your paths configured correctly in tsconfig.json compilerOptions, you don't really need this rule and you can just disable it. The integrated compiler always catches typos in import paths.
Therefore your best option is just:

rules: [
   "import/no-unresolved": "off",
]

It took me a while to figure that out, and I kicked myself when I did.

thank you bro, I did wonder why I'm spending 5+ hours trying to fix this rubbish when if the pathing was broken TS would of told me, I should of not been so stubborn, set and forget, how miserable HAHA

One hour ago, a new answer, so, the issue still happends.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests