import-js / import-js/eslint-plugin-import

Issue with local modules not being recognised unless ESLint parserOptions.sourceType is set to "script".

Open
#1,909 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
5.9k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Description

Packages used: -
babel-plugin-module-resolver v4.0.0
eslint-import-resolver-babel-module v5.1.2
eslint-plugin-import v2.22.0

I am unable to get ESLint to recognise the module import (Unable to resolve path to module '_components/TheThing'.eslint(import/no-unresolved)) unless I change parserOptions.sourceType to "script", which then messes up import statements in ES6 modules. All the local modules I am trying to import from are ES6 using export syntax.

What is the problem here? Why does parserOptions.sourceType being set to "module" cause this issue? Thanks.

.babelrc: -

{
  "plugins": [
    ["module-resolver", {
      "alias": {
        "_components": "./components"
      }
    }]
  ]
}

.eslintrc.js: -

module.exports = {
  parser: "@typescript-eslint/parser",
  parserOptions: {
    project: './tsconfig.json',
    "ecmaFeatures": {
      "jsx": true,
      "generators": false,
      "objectLiteralDuplicateProperties": false
    },
    "ecmaVersion": 6,
    "sourceType": "module"
  },
  env: {
    "browser": true,
    "es6": true,
    "node": true
  },
  "globals": {},
  extends: [
    "plugin:prettier/recommended",
    "plugin:import/errors",
    "plugin:import/warnings"
  ],
  settings: {
    react: {
      "version": "detect",
    },
    "import/resolver": {
      node: {
        extensions: ['.js', '.jsx', '.ts', '.tsx'],
        paths: ["./components"]
      },
      "babel-module": {}
    },
  },
  plugins: [
    "@typescript-eslint",
    "import",
    "jsx-a11y",
    "prettier",
    "react",
    "react-hooks"
  ],
}

tsconfig.json: -

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "_components/*": ["./components/*"],
    }
  }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied .eslintrc.js, .babelrc, and tsconfig.json, then reproduce import/no-unresolved for _components/TheThing with sourceType set to module. Check how the configured babel-module and node resolvers handle the TypeScript and JavaScript extensions. Done means the local ES6 module resolves without changing sourceType to script.

Written by the indexing model from the issue text.

Assessment

Tech stack
babel, eslint, javascript, typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.