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

Error `Cannot find module 'eslint/lib/util/source-code' #1307

Closed
guyfedwards opened this issue Mar 24, 2019 · 10 comments
Closed

Error `Cannot find module 'eslint/lib/util/source-code' #1307

guyfedwards opened this issue Mar 24, 2019 · 10 comments

Comments

@guyfedwards
Copy link

guyfedwards commented Mar 24, 2019

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
};
@ljharb
Copy link
Member

ljharb commented Mar 24, 2019

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

@guyfedwards
Copy link
Author

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

@ljharb
Copy link
Member

ljharb commented Mar 27, 2019

hmm, what version of eslint is installed?

@guyfedwards
Copy link
Author

eslint@5.15.3

@ljharb
Copy link
Member

ljharb commented Mar 28, 2019

So, that file exists: https://unpkg.com/eslint@5.15.3/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?

@guyfedwards
Copy link
Author

$ 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)

@ljharb
Copy link
Member

ljharb commented Mar 28, 2019

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.

@guyfedwards
Copy link
Author

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

randytarampi added a commit to randytarampi/generic-icon-splash-generate that referenced this issue Jul 5, 2019
randytarampi added a commit to randytarampi/pwa-asset-generator that referenced this issue Jul 5, 2019
randytarampi added a commit to randytarampi/pseudolocalize that referenced this issue Jul 5, 2019
randytarampi added a commit to randytarampi/pseudoimage that referenced this issue Jul 5, 2019
randytarampi added a commit to randytarampi/lwip that referenced this issue Jul 5, 2019
@rahgurung
Copy link

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

@jamonholmgren
Copy link

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",

randytarampi added a commit to randytarampi/lwip that referenced this issue Jan 10, 2020
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

4 participants