eslint-plugin-compat an not be resolved when import-x/no-unresolved for commonjs is activated
Given this config
```js
const { defineConfig } = require('eslint/config');
const jsPlugin = require('@eslint/js');
const { importX } = require('eslint-plugin-import-x');
const tsParser = require('@typescript-eslint/parser');
const globals = require('globals');
module.exports = defineConfig([
{
extends: [jsPlugin.configs.recommended, importX.flatConfigs.recommended],
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
languageOptions: {
parser: tsParser,
ecmaVersion: 'latest',
sourceType: 'module',
globals: { ...globals.node },
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
},
settings: {
'import-x/extensions': [
'.js',
'.mjs',
'.cjs',
'.jsx',
'.mjsx',
'.ts',
'.tsx',
'.cts',
'.mts',
],
'import-x/external-module-folders': [
'node_modules',
'node_modules/@types',
],
'import-x/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx', '.cts', '.mts'],
},
'import-x/resolver': {
typescript: true,
},
},
rules: {
'import-x/no-unresolved': [
'error',
{
commonjs: true,
caseSensitive: true,
},
],
},
},
]);
```
I have a simple file i want to lint.
```js
const globals = require('globals');
const compat = require('eslint-plugin-compat');
```
When i run eslint on this file i get following error
`3:24 error Unable to resolve path to module 'eslint-plugin-compat' import-x/no-unresolved`
It seems it has to do with the rule **'import-x/no-unresolved'**. I have enabled DEBUG and it tells me this
```
DEBUG resolve{path="C:\\Repos\\eslint-config\\configs" specifier="globals"}: unrs_resolver: options=ResolveOptions { cwd: None, tsconfig: None, alias: [], alias_fields: [], condition_names: ["types", "import", "esm2020", "es2020", "es2015", "require", "node", "node-addons", "browser", "default"], description_files: ["package.json"], enforce_extension: Disabled, exports_fields: [["exports"]], imports_fields: [["imports"]], extension_alias: [(".mts", [".mts", ".d.mts", ".mjs"]), (".tsx", [".tsx", ".d.ts", ".jsx", ".js"]), (".cts", [".cts", ".d.cts", ".cjs"]), (".cjs", [".cts", ".d.cts", ".cjs"]), (".jsx", [".tsx", ".d.ts", ".jsx"]), (".ts", [".ts", ".d.ts", ".js"]), (".js", [".ts", ".tsx", ".d.ts", ".js"]), (".mjs", [".mts", ".d.mts", ".mjs"])], extensions: [".ts", ".tsx", ".d.ts", ".js", ".jsx", ".json", ".node"], fallback: [], fully_specified: false, main_fields: ["types", "typings", "fesm2020", "fesm2015", "esm2020", "es2020", "module", "jsnext:main", "main"], main_files: ["index"], modules: ["node_modules"], resolve_to_context: false, prefer_relative: false, prefer_absolute: false, restrictions: [], roots: [], symlinks: true, builtin_modules: false, module_type: false, allow_package_exports_in_directory_resolve: false, yarn_pnp: false } path="C:\\Repos\\eslint-config\\configs" specifier="globals" ret="C:\\Repos\\eslint-config\\node_modules\\globals\\index.d.ts"
DEBUG resolve{path="C:\\Repos\\eslint-config\\configs" specifier="eslint-plugin-compat"}: unrs_resolver: options=ResolveOptions { cwd: None, tsconfig: None, alias: [], alias_fields: [], condition_names: ["types", "import", "esm2020", "es2020", "es2015", "require", "node", "node-addons", "browser", "default"], description_files: ["package.json"], enforce_extension: Disabled, exports_fields: [["exports"]], imports_fields: [["imports"]], extension_alias: [(".mts", [".mts", ".d.mts", ".mjs"]), (".tsx", [".tsx", ".d.ts", ".jsx", ".js"]), (".cts", [".cts", ".d.cts", ".cjs"]), (".cjs", [".cts", ".d.cts", ".cjs"]), (".jsx", [".tsx", ".d.ts", ".jsx"]), (".ts", [".ts", ".d.ts", ".js"]), (".js", [".ts", ".tsx", ".d.ts", ".js"]), (".mjs", [".mts", ".d.mts", ".mjs"])], extensions: [".ts", ".tsx", ".d.ts", ".js", ".jsx", ".json", ".node"], fallback: [], fully_specified: false, main_fields: ["types", "typings", "fesm2020", "fesm2015", "esm2020", "es2020", "module", "jsnext:main", "main"], main_files: ["index"], modules: ["node_modules"], resolve_to_context: false, prefer_relative: false, prefer_absolute: false, restrictions: [], roots: [], symlinks: true, builtin_modules: false, module_type: false, allow_package_exports_in_directory_resolve: false, yarn_pnp: false } path="C:\\Repos\\eslint-config\\configs" specifier="eslint-plugin-compat" err=NotFound("eslint-plugin-compat")
eslint-import-resolver-typescript unrs-resolver error: Cannot find module 'eslint-plugin-compat'
DEBUG resolve{path="C:\\Repos\\eslint-config\\configs" specifier="@types/eslint-plugin-compat"}: unrs_resolver: options=ResolveOptions { cwd: None, tsconfig: None, alias: [], alias_fields: [], condition_names: ["types", "import", "esm2020", "es2020", "es2015", "require", "node", "node-addons", "browser", "default"], description_files: ["package.json"], enforce_extension: Disabled, exports_fields: [["exports"]], imports_fields: [["imports"]], extension_alias: [(".mts", [".mts", ".d.mts", ".mjs"]), (".tsx", [".tsx", ".d.ts", ".jsx", ".js"]), (".cts", [".cts", ".d.cts", ".cjs"]), (".cjs", [".cts", ".d.cts", ".cjs"]), (".jsx", [".tsx", ".d.ts", ".jsx"]), (".ts", [".ts", ".d.ts", ".js"]), (".js", [".ts", ".tsx", ".d.ts", ".js"]), (".mjs", [".mts", ".d.mts", ".mjs"])], extensions: [".ts", ".tsx", ".d.ts", ".js", ".jsx", ".json", ".node"], fallback: [], fully_specified: false, main_fields: ["types", "typings", "fesm2020", "fesm2015", "esm2020", "es2020", "module", "jsnext:main", "main"], main_files: ["index"], modules: ["node_modules"], resolve_to_context: false, prefer_relative: false, prefer_absolute: false, restrictions: [], roots: [], symlinks: true, builtin_modules: false, module_type: false, allow_package_exports_in_directory_resolve: false, yarn_pnp: false } path="C:\\Repos\\eslint-config\\configs" specifier="@types/eslint-plugin-compat" err=NotFound("@types/eslint-plugin-compat")
eslint-import-resolver-typescript unrs-resolver error: Cannot find module '@types/eslint-plugin-compat' +1ms
eslint-import-resolver-typescript didn't find eslint-plugin-compat with undefined
```
I don't know why it can't find **eslint-plugin-compat**. It is installed under **node_modules**. For example it can perfectly resolve the **globals** module.
关闭于 2025-08-22 1 条评论