Create React App提供的react-scripts包需要依赖:Babel-Jest

时间:2020-04-20 10:43:26

标签: html reactjs jestjs babeljs

我在运行react-script test时发现了此错误消息

The react-scripts package provided by Create React App requires a dependency“ babel-jest”:“ ^ 24.9.0”`

因此,该错误消息还建议我们删除package-lock.json以及我尝试过的node_modules,但是我有相同的错误消息。

然后我检查package-lock.json,例如jest-config(是子依赖项)使用的版本高于react-script接受的版本。

如果您有任何建议,请

packages.json

{
  "name": "creator-web-app",
  "version": "0.1.0",
  "private": true,
  "main": "public/electron.js",
  "homepage": "./",
  "dependencies": {
    "@craco/craco": "^5.6.4",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "@types/react": "^16.9.34",
    "await-to-js": "^2.1.1",
    "axios": "^0.19.2",
    "classnames": "^2.2.6",
    "electron-is-dev": "^1.2.0",
    "env-cmd": "^10.1.0",
    "enzyme": "^3.11.0",
    "history": "^4.10.1",
    "i18next": "^19.4.2",
    "i18next-browser-languagedetector": "^4.0.2",
    "jest": "^25.3.0",
    "jwt-decode": "^2.2.0",
    "moment": "^2.24.0",
    "nano-id": "^1.1.0",
    "npm-watch": "^0.6.0",
    "react": "^16.13.1",
    "react-cookies": "^0.1.1",
    "react-dom": "^16.13.1",
    "react-i18next": "^11.3.4",
    "react-inlinesvg": "^1.2.0",
    "react-redux": "^7.2.0",
    "react-router-dom": "5.1.2",
    "react-scripts": "3.4.1",
    "redux": "^4.0.5",
    "redux-form": "^8.3.3",
    "redux-thunk": "^2.3.0",
    "typescript": "^3.7.0"
  },
  "scripts": {
    "electron": "NODE_ENV=production npm run build && NODE_ENV=production electron .",
    "start": "NODE_ENV=development react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "pack": "NODE_ENV=production && npm run build && electron-builder --dir",
    "dist": "NODE_ENV=production && npm run build electron-builder"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@types/classnames": "^2.2.10",
    "@types/enzyme": "^3.10.5",
    "@types/jest": "^25.2.1",
    "@types/react-redux": "^7.1.7",
    "@types/react-router-dom": "^5.1.4",
    "@types/redux-form": "^8.2.3",
    "craco-alias": "^2.1.1",
    "electron": "^8.2.1",
    "electron-builder": "^22.4.1",
    "enzyme-adapter-react-16": "^1.15.2",
    "node-sass": "^4.13.1",
    "react-test-renderer": "^15.6.2",
    "ts-jest": "^25.4.0",
    "whatwg-fetch": "^3.0.0"
  },
  "build": {
    "appId": "studioapp.id",
    "mac": {
      "category": "your.app.category.type"
    },
    "files": [
      "**/*"
    ],
    "directories": {
      "buildResources": "."
    },
    "protocols": {
      "name": "studioapp-protocol",
      "schemes": [
        "studioapp"
      ]
    }
  },
  "jest": {
    "collectCoverageFrom": [
      "<rootDir>/src/**/*.{ts,tsx}"
    ],
    "moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga|yml)$": "<rootDir>/tests/test-file-mock.js",
      "\\.(css|less|scss|sss|styl)$": "<rootDir>/tests/test-mock.js",
      "@/(.*)$": "<rootDir>/src/$1"
    },
    "transform": {
      "^.+\\.(tsx|ts)?$": "ts-jest"
    }
  }
}

谢谢

5 个答案:

答案 0 :(得分:2)

create-react-app遇到了同样的问题。这是我所做的

转到路径

C:\Users\YOURNAME\node_modules

,然后删除那些显示错误的软件包。

此处提供更多信息:https://github.com/creativetimofficial/argon-dashboard-react/issues/28

答案 1 :(得分:1)

具有讽刺意味的是,今天早上我正在研究一个类似的问题。您能否提供package.json和错误消息的屏幕截图?

Babel-Jest用于将javascript代码编译为jest可以使用的格式。我怀疑您的问题是您依赖于锁定文件随后锁定了错误版本的babel-jest。

答案 2 :(得分:0)

我可以从上述响应中添加更多详细信息,以解决我发现的问题。

当我们使用react-scripts时,我们无法获得开玩笑的程序包,或者已经由create-react-app管理的东西

我遇到的问题是package.json包含比react-script可接受的更高版本的笑话

当我删除package.json中的这一行时,它似乎已经可以工作了

答案 3 :(得分:0)

您的一个依赖项是使用babel-jest的较新版本。您可以通过在项目根目录中运行npm ls babel-jest来找到它们。

然后解决此问题的一种简单方法是尝试链接到这些软件包的旧版本,例如"@types/jest": "^24.0.0",以便他们不使用更新版本的babel-jest

答案 4 :(得分:0)

mac 用户删除

/Users/{UserName}/node_modules