当前位置: 首页 > news >正文

ESLint 配置错误:ReferenceError: prettier is not defined 解决方案

问题描述

在使用 pnpm lint 运行 ESLint 时,出现以下错误:

Oops! Something went wrong! :(
ESLint: 9.31.0
ReferenceError: prettier is not defined

该错误导致 ESLint 无法正确执行代码格式检查,但 不会影响项目的实际运行(如 pnpm dev 或 pnpm build)。

错误原因

  1. eslint-plugin-prettier 未正确导入

    • 在 eslint.config.js 中使用了 prettier/prettier 规则,但没有正确引入 eslint-plugin-prettier

  2. ESLint v9+ 的 Flat Config 格式兼容性问题

    • 旧版 ESLint 的配置方式(如 .eslintrc.js)与新版 eslint.config.js 不兼容。

解决方案

方法 1:修复 eslint.config.js(推荐)

1. 安装必要依赖
pnpm add -D eslint-plugin-prettier prettier
2. 修改 eslint.config.js
import { defineConfig } from "eslint/config";
import globals from "globals";
import js from "@eslint/js";
import pluginVue from "eslint-plugin-vue";
import prettierPlugin from "eslint-plugin-prettier"; // 正确导入 Prettier 插件export default defineConfig([{files: ["**/*.{js,mjs,jsx,vue}"],ignores: ["**/dist/**", "**/dist-ssr/**", "**/coverage/**"],},{languageOptions: {globals: {...globals.browser,},},},js.configs.recommended,...pluginVue.configs["flat/essential"],{plugins: {prettier: prettierPlugin, // 注册 Prettier 插件},rules: {"prettier/prettier": ["warn",{singleQuote: true,semi: false,printWidth: 60,trailingComma: "none",endOfLine: "auto",},],"vue/multi-word-component-names": ["warn",{ ignores: ["index"] },],"vue/no-setup-props-destructure": "off","no-undef": "error",},},
]);
3. 重新运行 ESLint
pnpm lint

方法 2:降级 ESLint(兼容旧配置)

如果不想使用 ESLint v9+ 的 Flat Config,可以降级到 ESLint v8:

pnpm add eslint@8.56.0 -D

然后改用 .eslintrc.js 配置(旧版格式):

module.exports = {extends: ["eslint:recommended","plugin:vue/essential","plugin:prettier/recommended", // 使用 Prettier 推荐配置],rules: {"prettier/prettier": ["warn",{singleQuote: true,semi: false,printWidth: 60,trailingComma: "none",endOfLine: "auto",},],"vue/multi-word-component-names": ["warn",{ ignores: ["index"] },],"vue/no-setup-props-destructure": "off","no-undef": "error",},
};


文章转载自:
http://become.tmizpp.cn
http://celebrator.tmizpp.cn
http://bawdy.tmizpp.cn
http://alderfly.tmizpp.cn
http://aerocamera.tmizpp.cn
http://benthonic.tmizpp.cn
http://apologete.tmizpp.cn
http://adulator.tmizpp.cn
http://chlordiazepoxide.tmizpp.cn
http://bearcat.tmizpp.cn
http://acantha.tmizpp.cn
http://cariban.tmizpp.cn
http://cherimoya.tmizpp.cn
http://byway.tmizpp.cn
http://abrasive.tmizpp.cn
http://anglerfish.tmizpp.cn
http://cayenne.tmizpp.cn
http://antilitter.tmizpp.cn
http://cherish.tmizpp.cn
http://barnstormer.tmizpp.cn
http://affiant.tmizpp.cn
http://appaloosa.tmizpp.cn
http://bulbar.tmizpp.cn
http://ambeer.tmizpp.cn
http://aliquot.tmizpp.cn
http://alcidine.tmizpp.cn
http://agilely.tmizpp.cn
http://caddish.tmizpp.cn
http://blonde.tmizpp.cn
http://cascalho.tmizpp.cn
http://www.dtcms.com/a/281178.html

相关文章:

  • 运维技术教程之Jenkins上的known_hosts文件
  • 高频高密度趋势下磁芯材料评价指标探讨
  • UVM(1)—配置环境
  • 算法学习day16----Python数据结构--模拟队列
  • 传统三层网络架构和现代数据中心网络架构(如思科 ACI 的 Spine-Leaf 架构)的对比和分析(Grok3 回答)
  • Mac电脑上无需卸载即可切换使用多个EasyConnect客户端的解决方案
  • Docker安装升级redis,并设置持久化
  • 使用 sudo iftop -i 分析服务器带宽使用情况
  • 一文读懂语义解析技术:从规则到神经网络的演进与挑战
  • PPP 链路及 MP 捆绑与 CHAP 验证实验
  • 从零开始学前端html篇3
  • Missing classes detected while running R8解决
  • 创客匠人:从 IP 到变现,定位是构建价值闭环的核心
  • Elasticsearch的深度翻页问题
  • Git本地操作完全指南:从入门到精通
  • 创客匠人:创始人 IP 打造,知识变现的时代必然
  • Elasticsearch 9.x 搜索执行流程(源码解读)
  • 深度学习中的激活函数:从原理到 PyTorch 实战
  • CentOS服务器安装Supervisor使队列可以在后台运行
  • 【用unity实现100个游戏之33】用Unity手搓一个类【红色警戒|魔兽争霸|帝国时代|星际争霸】3D RTS游戏(附源码)
  • vue openlayer创建地图弹框overlay
  • 【html常见页面布局】
  • [ROS 系列学习教程] ROS动作通讯(Action):通信模型、Hello World与拓展
  • k8s环境使用Operator部署Seaweedfs集群(下)
  • 【鸿蒙HarmonyOS】鸿蒙app开发入门到实战教程(三):实现一个音乐列表的页面
  • Flutter Socket 连接方案分析与适用场景
  • RestTemplate 实现后端 HTTP 调用详解
  • spring-ai-alibaba 多模态之音频
  • 前端Vue.js面试题(4)
  • 超详细 anji-captcha滑块验证springboot+uniapp微信小程序前后端组合