npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eslint-import-resolver-typescript

v3.7.0

Published

This plugin adds `TypeScript` support to `eslint-plugin-import`

Downloads

42,692,808

Readme

eslint-import-resolver-typescript

GitHub Actions type-coverage npm GitHub Release

Conventional Commits Renovate enabled JavaScript Style Guide Code Style: Prettier changesets

This plugin adds TypeScript support to eslint-plugin-import (Or maybe you want to try eslint-plugin-i for faster speed)

This means you can:

  • import/require files with extension .cts/.mts/.ts/.tsx/.d.cts/.d.mts/.d.ts
  • Use paths defined in tsconfig.json
  • Prefer resolving @types/* definitions over plain .js/.jsx
  • Multiple tsconfigs support just like normal
  • imports/exports fields support in package.json

TOC

Notice

After version 2.0.0, .d.ts will take higher priority then normal .js/.jsx files on resolving node_modules packages in favor of @types/* definitions or its own definition.

If you're facing some problems on rules import/default or import/named from eslint-plugin-import, do not post any issue here, because they are just working exactly as expected on our sides, take import-js/eslint-plugin-import#1525 as reference or post a new issue to eslint-plugin-import instead.

Installation

# npm
npm i -D eslint-plugin-import eslint-import-resolver-typescript

# pnpm
pnpm i -D eslint-plugin-import eslint-import-resolver-typescript

# yarn
yarn add -D eslint-plugin-import eslint-import-resolver-typescript

Important when using eslint-plugin-i and npm: Use npm i -D eslint-plugin-import@eslint-plugin-i@latest eslint-import-resolver-typescript, or you will end up with both eslint-plugin-import and eslint-plugin-i in your node_modules.

Configuration

eslint.config.js

If you are using eslint-plugin-import-x@>=4.5.0, you can use import/require to reference eslint-import-resolver-typescript directly in your ESLint flat config:

// eslint.config.js
const {
  createTypeScriptImportResolver,
} = require('eslint-import-resolver-typescript')

module.exports = [{
  settings: {
    "import/resolver-next": [
      createTypeScriptImportResolver({
        alwaysTryTypes: true, // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist`

        // Choose from one of the "project" configs below or omit to use <root>/tsconfig.json by default

        // use <root>/path/to/folder/tsconfig.json
        project: "path/to/folder",

        // Multiple tsconfigs (Useful for monorepos)

        // use a glob pattern
        project: "packages/*/tsconfig.json",

        // use an array
        project: [
          "packages/module-a/tsconfig.json",
          "packages/module-b/tsconfig.json"
        ],

        // use an array of glob patterns
        project: [
          "packages/*/tsconfig.json",
          "other-packages/*/tsconfig.json"
        ]
      }),
    ];
  }
}]

But if you are using eslint-plugin-import or the older version of eslint-plugin-import-x, you can't use require/import:

// eslint.config.js
module.exports = [
  {
    settings: {
      'import/resolvers': {
        typescript: {
          alwaysTryTypes: true, // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist`

          // Choose from one of the "project" configs below or omit to use <root>/tsconfig.json by default

          // use <root>/path/to/folder/tsconfig.json
          project: 'path/to/folder',

          // Multiple tsconfigs (Useful for monorepos)

          // use a glob pattern
          project: 'packages/*/tsconfig.json',

          // use an array
          project: [
            'packages/module-a/tsconfig.json',
            'packages/module-b/tsconfig.json',
          ],

          // use an array of glob patterns
          project: [
            'packages/*/tsconfig.json',
            'other-packages/*/tsconfig.json',
          ],
        },
      },
    },
  },
]

.eslintrc

Add the following to your .eslintrc config:

{
  "plugins": ["import"],
  "rules": {
    // turn on errors for missing imports
    "import/no-unresolved": "error"
  },
  "settings": {
    "import/parsers": {
      "@typescript-eslint/parser": [".ts", ".tsx"]
    },
    "import/resolver": {
      "typescript": {
        "alwaysTryTypes": true, // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist`

        // Choose from one of the "project" configs below or omit to use <root>/tsconfig.json by default

        // use <root>/path/to/folder/tsconfig.json
        "project": "path/to/folder",

        // Multiple tsconfigs (Useful for monorepos)

        // use a glob pattern
        "project": "packages/*/tsconfig.json",

        // use an array
        "project": [
          "packages/module-a/tsconfig.json",
          "packages/module-b/tsconfig.json"
        ],

        // use an array of glob patterns
        "project": [
          "packages/*/tsconfig.json",
          "other-packages/*/tsconfig.json"
        ]
      }
    }
  }
}

Options from enhanced-resolve

conditionNames

Default:

[
  "types",
  "import",

  // APF: https://angular.io/guide/angular-package-format
  "esm2020",
  "es2020",
  "es2015",

  "require",
  "node",
  "node-addons",
  "browser",
  "default"
]

extensions

Default:

[
  // `.mts`, `.cts`, `.d.mts`, `.d.cts`, `.mjs`, `.cjs` are not included because `.cjs` and `.mjs` must be used explicitly
  ".ts",
  ".tsx",
  ".d.ts",
  ".js",
  ".jsx",
  ".json",
  ".node"
]

extensionAlias

Default:

{
  ".js": [
    ".ts",
    // `.tsx` can also be compiled as `.js`
    ".tsx",
    ".d.ts",
    ".js"
  ],
  ".jsx": [".tsx", ".d.ts", ".jsx"],
  ".cjs": [".cts", ".d.cts", ".cjs"],
  ".mjs": [".mts", ".d.mts", ".mjs"]
}

mainFields

Default:

[
  "types",
  "typings",

  // APF: https://angular.io/guide/angular-package-format
  "fesm2020",
  "fesm2015",
  "esm2020",
  "es2020",

  "module",
  "jsnext:main",

  "main"
]

Other options

You can pass through other options of enhanced-resolve directly

Default options

You can reuse defaultConditionNames, defaultExtensions, defaultExtensionAlias and defaultMainFields by require/import them directly

Contributing

  • Make sure your change is covered by a test import.
  • Make sure that yarn test passes without a failure.
  • Make sure that yarn lint passes without conflicts.
  • Make sure your code changes match our type-coverage settings: yarn type-coverage.

We have GitHub Actions which will run the above commands on your PRs.

If either fails, we won't be able to merge your PR until it's fixed.

Sponsors

| 1stG | RxTS | UnTS | | ---------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | | 1stG Open Collective backers and sponsors | RxTS Open Collective backers and sponsors | UnTS Open Collective backers and sponsors |

Backers

| 1stG | RxTS | UnTS | | -------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- | | 1stG Open Collective backers and sponsors | RxTS Open Collective backers and sponsors | UnTS Open Collective backers and sponsors |

Changelog

Detailed changes for each release are documented in CHANGELOG.md.

License

ISC