import-js / eslint-plugin-import

ESLint plugin with rules that help validate proper imports.
MIT License
5.55k stars 1.56k forks source link

"Unable to resolve path to module" to many npm packages #2423

Closed goldylucks closed 2 years ago

goldylucks commented 2 years ago

There are many npm packages that the plugin can't find, i.e.

import "dotenv/config"
import "newrelic"
import express from 'express'
import cookieParser from "cookie-parser"

any ideas why is that?

The code works in production so I know it's a problem with the plugin and not the code itself.

Here's the eslint file

/* eslint-disable @typescript-eslint/no-var-requires */
const path = require("path")

// eslint-disable-next-line no-undef
module.exports = {
  extends: getExtends(),
  plugins: getPlugins(),
  rules: getRules(),
  parserOptions: getParserOptions(),
  parser: "@typescript-eslint/parser",
  env: getEnv(),
  settings: getSettings(),
}

function getExtends() {
  return ["plugin:@typescript-eslint/recommended", "plugin:import/recommended"]
}

function getPlugins() {
  return ["@typescript-eslint"]
}

function getRules() {
  return {
    "object-shorthand": ["error", "always"],
    "no-console": ["error"],
    "prefer-const": 2,
    "@typescript-eslint/ban-ts-comment": 0,
    "@typescript-eslint/no-explicit-any": 0,
    "@typescript-eslint/no-unused-vars": [
      "error",
      { vars: "all", args: "after-used", ignoreRestSiblings: true },
    ],
    "import/order": [
      "error",
      {
        groups: [
          "builtin",
          "external",
          "internal",
          "index",
          "parent",
          "sibling",
        ],
        "newlines-between": "always",
      },
    ],
  }
}

function getParserOptions() {
  return {
    ecmaVersion: 12,
    // eslint-disable-next-line no-undef
    project: [path.resolve(__dirname, "tsconfig.json")],
  }
}

function getEnv() {
  return {
    es2021: true,
    jasmine: true,
  }
}

function getSettings() {
  return {
    "import/parsers": {
      "@typescript-eslint/parser": [".ts", "js"],
    },
    "import/resolver": {
      node: {
        extensions: [".ts", "js"],
      },
    },
  }
}

and the tsconfig

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports": true,
    "strict": false,
    "strictPropertyInitialization": false,
    "sourceMap": true,
    "noEmit": true,
    "experimentalDecorators": true,
    "rootDirs": ["src", "local", "tests"],
    "skipLibCheck": true,
    "typeRoots": ["src/@types", "node_modules/@types"]
  },
  "exclude": ["node_modules"],
  "include": [
    "src/**/*",
    "local/**/*",
    "tests/**/*",
    "jest.config.ts",
    ".eslintrc.js"
  ]
}

and tsconfig.eslint,.js

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "noEmit": true,
    "rootDirs": ["src", "local", "tests"]
  },
  "include": ["src/**/*", "local/**/*", "tests/**/*"]
}

THanks!

goldylucks commented 2 years ago

Any updates here guys? this is happening to about 30% of npm packages

ljharb commented 2 years ago

That’s a pretty over exaggerated number; it’s probably far less than 0.001% of all npm packages that are using “exports” in a non-backwards-compatible way (which i suspect is the problem here).

ljharb commented 2 years ago

What error do you get? Any chance you could make a repro repo?

https://unpkg.com/browse/dotenv@16.0.0/package.json for example should work just fine.

goldylucks commented 2 years ago

there you go https://github.com/goldylucks/eslint-plugin-import-fail

image image image image
JounQin commented 2 years ago

close in favor of https://github.com/import-js/eslint-import-resolver-typescript