Eslint-plugin-import: import/no-unused-modules crashes linter

Created on 13 Jun 2019  路  11Comments  路  Source: benmosher/eslint-plugin-import

Error: TypeError: Error while loading rule 'import/no-unused-modules': Cannot read property 'path' of null

.eslintrc

{
  "extends": [
    "react-app",
    "plugin:prettier/recommended",
    "plugin:cypress/recommended",
    "eslint:recommended",
    "plugin:import/errors",
    "plugin:import/warnings"
  ],
  "settings": {
    "import/resolver": {
      "node": {
        "moduleDirectory": ["node_modules", "src"]
      }
    }
  },
  "globals": {
    "_": true,
    "less": true,
    "moment": true,
    "cypress/globals": true
  },
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaVersion": 2018,
    "ecmaFeatures": {
      "impliedStrict": true,
      "classes": true
    }
  },
  "env": {
    "browser": true,
    "node": true,
    "jest": true
  },
  "rules": {
    "no-console": ["error", { "allow": ["warn", "error"] }],
    "array-callback-return": "error",
    "no-unused-vars": "warn",
    "eqeqeq": "error",
    "no-eq-null": "error",
    "no-lone-blocks": "error",
    "no-magic-numbers": "warn",
    "no-shadow": "warn",
    "max-depth": ["error", 3],
    "import/order": [
      "error",
      { "groups": ["builtin", "external", "internal", "index", "parent", "sibling"], "newlines-between": "always" }
    ],
    "import/newline-after-import": ["error"],
    "import/no-unused-modules": [1, { "unusedExports": true }]
  }
}
bug help wanted typescript

Most helpful comment

Sorry for my late response, I found the root of the issue, I had an export * from 'redux-starter-kit' somewhere in my code.
I think it cause some empty value() because it export Typescript interfaces.

I should probably be fine to add a null check for the value()

I had a second issue that appears sometimes when it do an export * from './SomeReactComponent'

TypeError: Error while loading rule 'import/no-unused-modules': Cannot read property 'get' of undefined
Occurred while linting front\packages\bu\misc\bank-logo\index.js
    at front\node_modules\eslint-plugin-import\lib\rules\no-unused-modules.js:196:44
    at Set.forEach (<anonymous>)
    at front\node_modules\eslint-plugin-import\lib\rules\no-unused-modules.js:193:11
    at Map.forEach (<anonymous>)

All 11 comments

Any idea on which file it errors? Does the error have a stack trace?

Hi :)

I just had the same issue.

TypeError: Error while loading rule 'import/no-unused-modules': Cannot read property 'path' of null
    at dependencies.forEach.value (\front\node_modules\eslint-plugin-import\lib\rules\no-unused-modules.js:115:37)
    at Set.forEach (<anonymous>)
    at srcFiles.forEach.file (\front\node_modules\eslint-plugin-import\lib\rules\no-unused-modules.js:114:20)
    at Set.forEach (<anonymous>)
    at prepareImportsAndExports (\front\node_modules\eslint-plugin-import\lib\rules\no-unused-modules.js:101:12)
    at doPreparation (\front\node_modules\eslint-plugin-import\lib\rules\no-unused-modules.js:223:3)
    at Object.create (\front\node_modules\eslint-plugin-import\lib\rules\no-unused-modules.js:315:7)
    at createRuleListeners (\front\node_modules\eslint\lib\linter.js:577:21)
    at Object.keys.forEach.ruleId (\front\node_modules\eslint\lib\linter.js:731:31)
    at Array.forEach (<anonymous>)

It is my first try for this plugin. I use a monorepo maybe it can cause this error.
I run it on my complete codebase so no idea if it comes from a particular file :/

Great! Can you make a PR with the fix and a test case?

I'm looking for the root cause to create a proper test. But I don't know why it happens for now.
@ljharb Do you know some cases when value() return null ?

I will investigate further next week

I don鈥檛, I鈥檓 afraid.

@Taranys can you strip down the affected project to share it for reproduction purposes? If I would have something to reproduce this issue, I would also try to dig into it.

Sorry for my late response, I found the root of the issue, I had an export * from 'redux-starter-kit' somewhere in my code.
I think it cause some empty value() because it export Typescript interfaces.

I should probably be fine to add a null check for the value()

I had a second issue that appears sometimes when it do an export * from './SomeReactComponent'

TypeError: Error while loading rule 'import/no-unused-modules': Cannot read property 'get' of undefined
Occurred while linting front\packages\bu\misc\bank-logo\index.js
    at front\node_modules\eslint-plugin-import\lib\rules\no-unused-modules.js:196:44
    at Set.forEach (<anonymous>)
    at front\node_modules\eslint-plugin-import\lib\rules\no-unused-modules.js:193:11
    at Map.forEach (<anonymous>)

Also had this issue, anything in the works for a fix?

I have similar issue:

TypeError: Cannot read property 'name' of undefined
    at astNode.specifiers.forEach.specifier (/node_modules/eslint-plugin-import/lib/rules/no-unused-modules.js:600:40)
    at Array.forEach (<anonymous>)

https://github.com/benmosher/eslint-plugin-import/blob/master/src/rules/no-unused-modules.js#L562

@darkowic can you share some minimal project to reproduce the issue?

Was this page helpful?
0 / 5 - 0 ratings