You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Node环境中babel-plugin-module-resolver与ESLint配合异常问题排查

问题:ESLint无法识别babel-plugin-module-resolver的别名配置

我已经在Node.js应用中完成了ESLint配置,现在打算用babel-plugin-module-resolver来设置模块别名,但导入模块时一直报错:Unable to resolve path to module 'middleware'。以下是我的配置文件信息:

package.json依赖片段

"dependencies": { 
  "@hapi/joi": "16.1.4", 
  "bcrypt": "3.0.6", 
  "config": "3.2.2", 
  "express": "4.17.1", 
  "jsonwebtoken": "8.5.1", 
  "mongoose": "5.7.1", 
  "request": "2.88.0", 
  "winston": "3.2.1", 
  "yarn": "1.17.3" 
}, 
"devDependencies": { 
  "@babel/cli": "7.6.2", 
  "@babel/core": "7.6.2", 
  "@babel/node": "7.6.2", 
  "@babel/preset-env": "7.6.2", 
  "babel-eslint": "10.0.3", 
  "babel-plugin-module-resolver": "^3.2.0", 
  "concurrently": "4.1.2", 
  "eslint": "6.5.1", 
  "eslint-config-airbnb": "18.0.1", 
  "eslint-import-resolver-babel-module": "^5.1.0", 
  "eslint-plugin-import": "2.18.2", 
  "nodemon": "1.19.2" 
}

.eslintrc配置

module.exports = { 
  env: { es6: true, node: true, }, 
  extends: [ 'airbnb-base', ], 
  globals: { Atomics: 'readonly', SharedArrayBuffer: 'readonly', }, 
  parserOptions: { ecmaVersion: 2018, sourceType: 'module', }, 
  rules: { 
    "indent": ["warn", 2], 
    "quotes": ["warn", "single"], 
    "no-unused-vars": ["error", {"warn": "none"}], 
    "max-len": ["warn", 150], 
    "no-console": 1, 
    "object-curly-newline": ["warn", { 
      "ObjectPattern": { "multiline": true }, 
      "ExportDeclaration": { "multiline": true, "minProperties": 4 } 
    }], 
  }, 
};

.babelrc配置

{ 
  "presets": ["@babel/preset-env"], 
  "plugins": [ 
    ["babel-plugin-module-resolver", { 
      "alias": { 
        "root": "./", 
        "middleware": "./middleware" 
      } 
    }] 
  ] 
}

请问哪里配置出错了?


解决方案:让ESLint识别Babel别名

看起来问题出在ESLint还没和你的Babel别名配置打通——虽然你装了eslint-import-resolver-babel-module,但没告诉ESLint要使用这个解析器来处理别名。按下面的步骤调整配置就能解决:

1. 更新.eslintrc,添加解析器配置

在你的.eslintrc里加入settings字段,指定import插件使用babel-module解析器,同时同步你的别名规则:

module.exports = { 
  env: { es6: true, node: true, }, 
  extends: [ 'airbnb-base', ], 
  globals: { Atomics: 'readonly', SharedArrayBuffer: 'readonly', }, 
  parserOptions: { ecmaVersion: 2018, sourceType: 'module', },
  // 新增这部分配置
  settings: {
    'import/resolver': {
      'babel-module': {
        alias: {
          'root': './',
          'middleware': './middleware'
        }
      }
    }
  },
  rules: { 
    "indent": ["warn", 2], 
    "quotes": ["warn", "single"], 
    "no-unused-vars": ["error", {"warn": "none"}], 
    "max-len": ["warn", 150], 
    "no-console": 1, 
    "object-curly-newline": ["warn", { 
      "ObjectPattern": { "multiline": true }, 
      "ExportDeclaration": { "multiline": true, "minProperties": 4 } 
    }], 
  }, 
};

2. 验证路径正确性

确认你的middleware文件夹确实在项目根目录(和.babelrc.eslintrc同一层级),如果路径有变动,记得同步修改.babelrc.eslintrc里的别名路径。

3. 重启ESLint服务

编辑器的ESLint缓存可能会导致新配置不生效,建议重启编辑器,或者运行eslint --cache-clear命令手动清除缓存,之后再检查代码应该就不会报错了。

这样调整后,ESLint就能正确识别你通过babel-plugin-module-resolver设置的别名啦。

内容的提问来源于stack exchange,提问作者Shashika Virajh

火山引擎 最新活动