首页 > 解决方案 > 安装后新的 React 应用程序无法立即编译

问题描述

我使用 React Native 已经有一段时间了,但我想我会在 Web 上试用 React。所以我遵循了这个指南:https ://reactjs.org/docs/create-a-new-react-app.html但是在使用之后npx create-react-app react-try,导航到新文件夹,然后输入yarn start我收到这个错误消息:

Failed to compile.

./src/index.js 1:60
Module parse failed: Unexpected token (1:60)
File was processed with these loaders:
 * ./node_modules/@pmmmwh/react-refresh-webpack-plugin/loader/index.js
 * ./node_modules/react-scripts/node_modules/babel-loader/lib/index.js
You may need an additional loader to handle the result of these loaders.
> $RefreshRuntime$ = require('C:/Users/e096752/Documents/Cole's Git Repos/react-try/node_modules/react-refresh/runtime.js');
| $RefreshSetup$(module.id);

这是 create 方法中包含的所有内容: 项目

包.json

{
  "name": "react-try",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.2",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

如果您需要任何其他信息,请告诉我。我到底需要做什么?

标签: reactjsnpmcreate-react-appyarnpkg

解决方案


react-scripts 4.0.2 似乎有问题。一种解决方法是在 package.json 上手动将版本更改为 4.0.1,然后运行yarn install​​. 这样就行了!


推荐阅读