ESLint import resolver for ESM modules via package.json exports map.
Relies on NPM package resolve.exports https://github.com/lukeed/resolve.exports
See:
- import-js/eslint-plugin-import#1868
- import-js/eslint-plugin-import#1810
- browserify/resolve#224
- wooorm/import-meta-resolve#2
- eslint-community/eslint-plugin-n#4
- mysticatea/eslint-plugin-node#255
- mysticatea/eslint-plugin-node#244
- mysticatea/eslint-plugin-node#258
- https://github.com/webpack/enhanced-resolve
Ran into an issue where this resolver would crash other eslint rules because it was trying to resolve builtin modules incorrectly.
Specifically this error, for
import/no-cycle:Fixed it with the following snippet:
const path = require('path'); const { resolve: resolveExports } = require('resolve.exports'); +const { builtinModules } = require('module'); /** * @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)] }); + if (builtinModules.includes(moduleId)) { + return { found: false }; + } 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, };