Skip to content

Instantly share code, notes, and snippets.

@danielweck
Last active March 4, 2025 10:45

Revisions

  1. danielweck revised this gist Mar 30, 2022. 2 changed files with 18 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions .eslintrc.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,16 @@
    module.exports = {
    settings: {
    // optionally, if TypeScript project:
    // 'import/parsers': {
    // '@typescript-eslint/parser': ['.ts', '.tsx'],
    // },
    'import/resolver': {
    // optionally, if TypeScript project:
    // https://github.com/alexgorbatchev/eslint-import-resolver-typescript
    // typescript: {
    // alwaysTryTypes: false,
    // project: ['./PATH/TO/tsconfig.json'],
    // },
    [path.resolve('./eslint-plugin-import-resolver.js')]: { someConfig: 1 },
    },
    },
    8 changes: 8 additions & 0 deletions eslint-plugin-import-resolver.js
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,20 @@
    const path = require('path');
    const { resolve: resolveExports } = require('resolve.exports');

    // optionally handle NodeJS built-ins just in case not handled by another ESLint module resolver in the chain
    const { builtinModules } = require('module');
    const builtins = new Set(builtinModules);

    /**
    * @param {string} source source
    * @param {string} file file
    * @param {Object} _config config
    */
    const resolve = (source, file, _config) => {
    if (builtins.has(source)) {
    // return { found: false }; // this also works?
    return { found: true, path: null };
    }
    try {
    const moduleId = require.resolve(source, { paths: [path.dirname(file)] });
    return { found: true, path: moduleId };
  2. danielweck revised this gist Feb 23, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .eslintrc.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    module.exports = {
    settings: {
    'import/resolver': {
    [path.resolve('./eslint-plugin-import-resolver.cjs')]: { someConfig: 1 },
    [path.resolve('./eslint-plugin-import-resolver.js')]: { someConfig: 1 },
    },
    },
    },
  3. danielweck revised this gist Feb 23, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@ Relies on NPM package `resolve.exports` https://github.com/lukeed/resolve.export
    See:

    * https://github.com/import-js/eslint-plugin-import/issues/1868
    * https://github.com/import-js/eslint-plugin-import/issues/1810
    * https://github.com/browserify/resolve/pull/224
    * https://github.com/wooorm/import-meta-resolve/issues/2
    * https://github.com/weiran-zsd/eslint-plugin-node/pull/4
  4. danielweck revised this gist Feb 23, 2022. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -10,4 +10,5 @@ See:
    * https://github.com/weiran-zsd/eslint-plugin-node/pull/4
    * https://github.com/mysticatea/eslint-plugin-node/issues/255
    * https://github.com/mysticatea/eslint-plugin-node/issues/244
    * https://github.com/mysticatea/eslint-plugin-node/issues/258
    * https://github.com/mysticatea/eslint-plugin-node/issues/258
    * https://github.com/webpack/enhanced-resolve
  5. danielweck created this gist Feb 23, 2022.
    7 changes: 7 additions & 0 deletions .eslintrc.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    module.exports = {
    settings: {
    'import/resolver': {
    [path.resolve('./eslint-plugin-import-resolver.cjs')]: { someConfig: 1 },
    },
    },
    },
    13 changes: 13 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    ESLint import resolver for ESM modules via package.json exports map.

    Relies on NPM package `resolve.exports` https://github.com/lukeed/resolve.exports

    See:

    * https://github.com/import-js/eslint-plugin-import/issues/1868
    * https://github.com/browserify/resolve/pull/224
    * https://github.com/wooorm/import-meta-resolve/issues/2
    * https://github.com/weiran-zsd/eslint-plugin-node/pull/4
    * https://github.com/mysticatea/eslint-plugin-node/issues/255
    * https://github.com/mysticatea/eslint-plugin-node/issues/244
    * https://github.com/mysticatea/eslint-plugin-node/issues/258
    27 changes: 27 additions & 0 deletions eslint-plugin-import-resolver.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    const path = require('path');
    const { resolve: resolveExports } = require('resolve.exports');

    /**
    * @param {string} source source
    * @param {string} file file
    * @param {Object} _config config
    */
    const resolve = (source, file, _config) => {
    try {
    const moduleId = require.resolve(source, { paths: [path.dirname(file)] });
    return { found: true, path: moduleId };
    } catch (/** @type {any} */ err) {
    if (err.code === 'MODULE_NOT_FOUND' && err.path?.endsWith('/package.json')) {
    const { name, module, main, exports } = require(err.path);
    const resolved = resolveExports({ name, module, main, exports }, source);
    const moduleId = path.join(path.dirname(err.path), resolved);
    return { found: true, path: moduleId };
    }
    return { found: false };
    }
    };

    module.exports = {
    interfaceVersion: 2,
    resolve,
    };