ESLint Import Resolver for Node Modules

0.3.10 · maintenance · verified Sun Apr 19

eslint-import-resolver-node is a foundational package that provides Node.js-style module resolution for `eslint-plugin-import`. It enables ESLint to accurately locate imported modules, respecting standard Node.js resolution rules, including looking into `node_modules` directories, and honoring `package.json`'s `main` field. The current stable version, `0.3.10`, was last published in February 2021. Despite its infrequent updates, it is highly stable and widely adopted, functioning as a core component of the `eslint-plugin-import` ecosystem. Its primary differentiation lies in its direct emulation of Node's native resolution algorithm, which ensures consistent linting behavior with the runtime environment. The resolver supports customization through options such as `extensions`, `paths` (similar to `NODE_PATH`), and `moduleDirectory` for defining alternate `node_modules` locations or source roots.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a typical `.eslintrc.cjs` configuration using `eslint-import-resolver-node` to handle module resolution for `eslint-plugin-import`. It explicitly sets up common file extensions and custom module paths, which are frequent requirements in modern projects. It also includes basic `eslint-plugin-import` rules to highlight its interaction.

// .eslintrc.cjs (for CommonJS or mixed projects, or .eslintrc.js)
const path = require('node:path');

module.exports = {
  root: true,
  env: {
    browser: true,
    es2021: true,
    node: true,
  },
  extends: [
    'eslint:recommended',
    'plugin:import/recommended',
    // If using TypeScript, also extend 'plugin:import/typescript'
  ],
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module',
  },
  settings: {
    'import/resolver': {
      node: {
        // Explicitly list extensions, including common ones like .js, .jsx, .ts, .tsx
        // If this is set, default '.js' is *not* included automatically.
        extensions: ['.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs', '.json'],
        // Define custom paths for module resolution, similar to NODE_PATH
        paths: [path.resolve(__dirname, 'src'), path.resolve(__dirname, 'lib')],
        // Define alternative module directories (e.g., 'bower_components')
        moduleDirectory: ['node_modules', 'bower_components']
      },
      // If using TypeScript, also add the TypeScript resolver
      // typescript: {
      //   alwaysTryTypes: true, // Always try to resolve types for TS files
      // },
    },
    'import/extensions': ['.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs'], // Extensions for eslint-plugin-import rules
  },
  plugins: [
    'import',
    // If using TypeScript: '@typescript-eslint'
  ],
  rules: {
    'no-unused-vars': 'warn',
    'import/no-unresolved': ['error', { commonjs: true, amd: true }],
    'import/named': 'error',
    'import/namespace': 'error',
    'import/default': 'error',
    'import/export': 'error',
  },
};

view raw JSON →