创建 React 应用程序在"Starting the development server"时失败



我的 react 应用程序是使用创建 react 应用程序构建的,https://reactjs.org/docs/create-a-new-react-app.html 无法启动。

当我执行npm run start我的应用程序挂起并显示以下消息。

正在启动开发服务器...

包.json

{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"@types/jest": "^25.1.1",
"@types/node": "^13.7.0",
"@types/react": "^16.9.19",
"@types/react-dom": "^16.9.5",
"@types/react-router-dom": "^5.1.3",
"axios": "^0.19.2",
"lodash": "^4.17.15",
"node-sass": "^4.13.1",
"npm-check-updates": "^4.0.1",
"qs": "^6.9.1",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-router-dom": "^5.1.2",
"react-scripts": "3.3.1",
"typescript": "^3.7.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"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": {
"@semantic-release/gitlab": "^6.0.2",
"semantic-release": "^17.0.2"
},
"release": {
"branch": "master",
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
[
"@semantic-release/gitlab",
{
"gitlabUrl": "https://git.companyname.com"
}
]
]
}
}

节点版本:v12.14.1npm 版本:6.13.4

我已经尝试了几次,每次失败时它都会创建一个新的节点进程,我知道这一点,因为它要求我选择一个不同的端口。

到目前为止,我已经尝试了以下过程,没有任何运气

rm -rf node_modules
npm install
npm start

如果您在使用 creat-react-app 创建 react 应用程序后自己安装了一些软件包,这可能是因为其中一个已安装的软件包不起作用。 过去,我遇到过像你这样的问题。 那时,我已经安装了 react-pdf 包来显示 pdf。 结果,开发服务器没有启动,每当我再次运行时,它都会要求选择另一个端口。 删除该包后,它运行良好。

所以我认为包.json中可能没有工作模块。 在我看来,它可能是"qs"包装。

我复制了您的package.json文件并在我的机器上本地运行它。

首先我跑了npm install

然后,我创建了/src/index.js并从create-react-app中插入了以下默认代码:

import React from "react";
import ReactDOM from "react-dom";
ReactDOM.render(<div>We're in!</div>, document.getElementById("root"));

我还创建了/public/index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>

最后,我跑了npm start

应用程序运行良好,唯一的问题是过时的子依赖项(旧版本的corejs(

这让我认为问题可能出在您的本地设置中。

我建议清除您的 npm 缓存并重试。

若要清除缓存,请使用提升的权限运行npm cache clear --f

我的建议是启动一个新的应用程序目录,以防现有目录中有损坏的内容。

对我来说,事实证明重新启动我的计算机修复了它。

我仍然不知道造成这种情况的原因,因为环境保持不变,除了删除node_modules和 npm 安装之外,我什么也没做。

我通过安装以前版本的 npm 脚本来修复它 尝试使用这个:

npm install react-scripts@2.1.8

最新更新