Eslint-plugin-import: Error `Cannot find module 'eslint/lib/util/source-code'

Created on 24 Mar 2019  路  10Comments  路  Source: benmosher/eslint-plugin-import

I am getting the same error as reported in #1265.

My setup is a little different though, I am creating a shareable plugin with config.
Reverting to 2.14.0 fixes the problem for me but that is not an proper solution going forward.

I have all the dependencies, plugins/configs within my custom plugin and then eslint in the repo consuming the custom plugin so all plugins/configs should be using the same version of eslint as it is only installed once.

eslint-plugin-my-plugin:

const plugins = ["import"];
const configs = ["standard"];

// check if rule is already part of a plugin
function isPluginRule(ruleName) {
  for (const pluginName of plugins) {
    if (ruleName.includes(`${pluginName}/`)) {
      return true;
    }
  }
  return false;
}

// prefix plugin rules with name of plugin
let pluginRules = {};
for (const pluginName of plugins) {
  const plugin = require(`eslint-plugin-${pluginName}`);

  console.log(plugin.rules);
  Object.keys(plugin.rules).forEach(ruleName => {
    pluginRules[`${pluginName}/${ruleName}`] = plugin.rules[ruleName];
  });
}

// prefix config rules with name of config
let configRules = {};
for (const configName of configs) {
  const config = require(`eslint-config-${configName}`);

  Object.keys(config.rules).forEach(ruleName => {
    if (isPluginRule(ruleName)) {
      configRules[`my-plugin/${ruleName}`] = config.rules[ruleName];
    } else {
      configRules[ruleName] = config.rules[ruleName];
    }
  });
}

// plugins often have recommended rules, we want to use them but the
// rules are prefixed now so we must update the recommended rules names
// to reflect that
let pluginRecommendedRules = {};
const pluginRecommendedConfigs = ["prettier/recommended", "react/recommended"];

for (const recName of pluginRecommendedConfigs) {
  const [pluginName, configName] = recName.split("/");
  const plugin = require(`eslint-plugin-${pluginName}`);
  const recommendedRules = plugin.configs[configName].rules;

  Object.keys(recommendedRules).forEach(ruleName => {
    pluginRecommendedRules[`my-plugin/${ruleName}`] =
      recommendedRules[ruleName];
  });
}

module.exports = {
  configs: {
    basic: {
      plugins: ["my-plugin"],
      // if rules are coming from a plugin they need to be prefixed with
      // `my-plugin/...`
      rules: {
        ...configRules,
        ...pluginRecommendedRules
      },
      parserOptions: {
        ecmaVersion: 6,
        sourceType: "module",
        ecmaFeatures: {
          jsx: true
        }
      }
    }
  },
  rules: pluginRules
};

Most helpful comment

This is what worked for me, just remove node_modules folder and do npm install again.

All 10 comments

Is eslint a peer dep and dev dep of your plugin? It should not be a production dep.

Just a peer dep of the plugin and a devDep of the test package consuming the plugin.

hmm, what version of eslint is installed?

So, that file exists: https://unpkg.com/[email protected]/lib/util/source-code.js

Can you confirm that node_modules/eslint/package.json has 5.15.3, and the version from node_modules/eslint-plugin-import/package.json?

$ cat node_modules/eslint/package.json | grep version
"version": "5.15.3",

I have the custom plugin yarn linked so the deps haven't been hoisted from within the plugin, the following is the version in the linked module:

$ cat node_modules/eslint-plugin-my-plugin/node_modules/eslint-plugin-import/package.json | grep version
"version": "2.16.0",

Running eslint gives the error:

$ ./node_modules/.bin/eslint ./index.js
Error: Cannot read config file: /home/name/projects/eslint-plugin-iconic/index.js
Error: Cannot find module 'eslint/lib/util/source-code'
Referenced from: /home/name/projects/test/.eslintrc
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
    at Function.Module._load (internal/modules/cjs/loader.js:508:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/home/name/projects/eslint-plugin-iconic/node_modules/eslint-plugin-import/lib/ExportMap.js:20:19)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)

aha, that's the problem. when using npm link/yarn link, every single singleton must be linked as well - which includes eslint and every eslint plugin, and also inside all of these package folders - ie, there must only be one shared copy of eslint among the project, as well as all of the eslint-related dependencies.

Ah ok, wondered if it might be something to do with linking. Thanks for the help!

This is what worked for me, just remove node_modules folder and do npm install again.

I had to upgrade all eslint-* packages (and eslint itself) to the latest and it worked.

    "babel-eslint": "^10.0.3",
    "eslint": "^6.6.0",
    "eslint-config-prettier": "^6.5.0",
    "eslint-config-standard": "^14.1.0",
    "eslint-plugin-import": "^2.18.2",
    "eslint-plugin-node": "^10.0.0",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-standard": "^4.0.1",
Was this page helpful?
0 / 5 - 0 ratings