You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

是否存在ESLint规则强制使用ES6 import替代CommonJS require?

用ESLint强制使用ES6 import替代CommonJS require
  • 第一步:安装依赖插件
    要实现这个需求,需要用到eslint-plugin-import插件,先通过包管理器安装:

    npm install eslint-plugin-import --save-dev
    

    或者使用yarn:

    yarn add eslint-plugin-import --dev
    
  • 第二步:配置.eslintrc.json
    在你的配置文件中添加插件并启用对应规则,将规则级别设为error,这样出现require时就会触发错误提示:

    {
      "plugins": ["import"],
      "rules": {
        "import/no-commonjs": "error"
      }
    }
    
  • 规则说明:
    import/no-commonjs会检测所有CommonJS风格的语法,包括require()调用、module.exports、exports.xxx导出等,一旦发现就会抛出错误,强制团队统一使用ES6的import和export语法。

内容的提问来源于stack exchange,提问作者praveenp-syd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 06:58:11