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

【REACT18.x】creat-react-app在添加eslint时报错Environment key “jest/globals“ is unknown

今天在创建新项目的时候,给cra创建的项目添加eslint支持,出现如下报错

添加eslint

npx eslint --init

页面报错

Compiled with problems:
×
ERROR
[eslint] package.json » eslint-config-react-app/jest#overrides[0]:Environment key "jest/globals" is unknown

在这里插入图片描述
在这里插入图片描述
解决方案:
应该是环境变量里面出现了jest这个库,导致的问题
找到pachage.json文件去掉这个配置项
在这里插入图片描述
再次启动项目就可以了

配置文件修改

Line 6:9:  'a' is assigned a value but never used  @typescript-eslint/no-unused-vars

在这里插入图片描述
增加配置
在这里插入图片描述

import js from "@eslint/js";
import globals from "globals";
import tseslint from "typescript-eslint";
import pluginReact from "eslint-plugin-react";
import { defineConfig } from "eslint/config";export default defineConfig([{ files: ["**/*.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"], plugins: { js }, extends: ["js/recommended"] },{ files: ["**/*.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"], languageOptions: { globals: globals.browser } },tseslint.configs.recommended,pluginReact.configs.flat.recommended,{files: ["**/*.{ts,mts,cts,tsx}"],languageOptions: {parserOptions: {project: true,},},// 忽略测试文件ignores: ["./src/**/*.test.**"],rules: {"@typescript-eslint/no-unused-vars": "off"},},
]);

这样就去掉了警告信息

报错3:Parsing error: project was set to true but couldn’t find any tsconfig.json relative to

在运行npm run lint命令时的报错

> r18-ts-demo@0.1.0 lint
> eslint ./src/**/*.{ts,mts,cts,tsx} --fixWarning: React version not specified in eslint-plugin-react settings. See https://github.com/jsx-eslint/eslint-plugin-react#configuration .D:\mine\r18-ts-demo\src\App.test.tsx7:9  error  'linkElement' is assigned a value but never used  @typescript-eslint/no-unused-varsD:\mine\r18-ts-demo\src\App.tsx0:0  error  Parsing error: project was set to `true` but couldn't find any tsconfig.json relative to 'D:\mine\r18-ts-demo\src\App.tsx' within 'D:\mine\r18-ts-demo\'D:\mine\r18-ts-demo\src\index.tsx0:0  error  Parsing error: project was set to `true` but couldn't find any tsconfig.json relative to 'D:\mine\r18-ts-demo\src\index.tsx' within 'D:\mine\r18-ts-demo\'D:\mine\r18-ts-demo\src\react-app-env.d.ts0:0  error  Parsing error: project was set to `true` but couldn't find any tsconfig.json relative to 'D:\mine\r18-ts-demo\src\react-app-env.d.ts' within 'D:\mine\r18-ts-demo\'

在这里插入图片描述
意思是eslint的配置文件找不到tsconfig.json文件
在这里插入图片描述
所以我们需要将project:true修改下
在这里插入图片描述
这样就找到了项目根目录下的ts配置文件,再次校验即可

关于配置写错文件的报错

我在测试文件中,声明了一个变量,但是没用使用
在这里插入图片描述
eslint会报警告

根据提示添加了在eslint.config中添加了如下的配置

rules: {"@typescript-eslint/no-unused-vars": "off",}

重启后,却不生效,这点确实有点诡异
使用的是自动生成的配置文件,一时不知道问题出在哪里

后来排除错误原来规则应该写在package.json文件里面
在这里插入图片描述
再次重启,这个警告信息就没有!!所以这点要注意,写对文件位置

"eslintConfig": {"extends": ["react-app"],"rules": {"@typescript-eslint/no-unused-vars": "off"}
}
http://www.dtcms.com/a/287222.html

相关文章:

  • Spring AI 项目实战(十九):Spring Boot + AI + Vue3 + OSS + DashScope 构建多模态视觉理解平台(附完整源码)
  • 在 .NET Core 中创建 Web Socket API
  • Redis 如何保证高并发与高可用
  • Elasticsearch 重命名索引
  • OllyDbg技巧学习
  • Go-Redis × 向量检索实战用 HNSW 在 Redis 中索引与查询文本 Embedding(Hash JSON 双版本)
  • 子线程不能直接 new Handler(),而主线程可以
  • LLM大模型微调技术与最佳实践
  • C语言:20250719笔记
  • 目标检测中的标签分配算法总结
  • 【OD机试】停车场收费
  • OpenCV特征点提取算法orb、surf、sift对比
  • Netty网络聊天室及扩展序列化算法
  • 2025年睿抗机器人开发者大赛CAIP-编程技能赛(省赛)-RoboCom 世界机器人开发者大赛-本科组
  • 微软原版系统下载的几个好用网站
  • LVS——nat模式
  • 【ArcGIS技巧】最近分享的GIS插件总结与优化
  • 实战分享:Web3 前端开发Defi项目
  • 【设计模式C#】外观模式(用于解决客户端对系统的许多类进行频繁沟通)
  • 【Unity编辑器开发GUI.Window】
  • 企业运维实战:Jenkins 依赖 JDK21 与应用需 JDK1.8 共存方案(含流水线配置)
  • 软件工程:可行性分析的任务及报告
  • 【NLP舆情分析】基于python微博舆情分析可视化系统(flask+pandas+echarts) 视频教程 - 基于wordcloud库实现词云图
  • 使用nvm安装node、npm、pnpm以及编译项目教程
  • 告别项目混乱:基于 pnpm + Turborepo 的现代化 Monorepo 工程化最佳实践
  • 【音视频协议篇】RTSP系列
  • gitlab+jenkins
  • 综合实验--eNSP实验
  • k8s 基本架构
  • Flowable31动态表单-----------------------终章