react脚本 3.4.0 的 npm start 问题

IT技术 node.js reactjs npm
2021-05-19 23:13:33

我知道已经有人问过这个问题,但我找不到解决方案。当我npm start在 React 项目中运行时,我总是得到相同的日志(见下文)。给出的唯一解决方案是卸载node_modules并重新安装。我也尝试清除缓存。没有任何效果。

有些人解决了升级到react-scripts 3.4.0https://github.com/facebook/create-react-app/issues/8490)的这个问题,但这对我不起作用。唯一的解决办法我现在已经是降级react-scripts到的版本2.1.8使用npm install react-scripts@2.1.8但这不是一个好的解决方案,因为我必须在使用最新版本的框架上工作。

这是日志:

i 「wds」: Project is running at http://192.168.43.27/
i 「wds」: webpack output is served from /
i 「wds」: Content not from webpack is served from C:\Users\fthom\Desktop\temp\my-app\public
i 「wds」: 404s will fallback to /index.html
Starting the development server...

events.js:288
      throw er; // Unhandled 'error' event
      ^

Error: spawn cmd ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:267:19)
    at onErrorNT (internal/child_process.js:469:16)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
Emitted 'error' event on ChildProcess instance at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:273:12)
    at onErrorNT (internal/child_process.js:469:16)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  errno: 'ENOENT',
  code: 'ENOENT',
  syscall: 'spawn cmd',
  path: 'cmd',
  spawnargs: [ '/s', '/c', 'start', '""', '/b', '"http://localhost:3000/"' ]
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! my-app@0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the my-app@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\fthom\AppData\Roaming\npm-cache\_logs\2020-02-21T10_59_13_574Z-debug.log
4个回答

我终于找到了解决方案。在机器 PATH 中添加默认变量:

C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem

变量位置的屏幕截图

改变路径对我有用。只是补充一下,对于那些不知道如何更改路径(与我相同)和更多细节的人的上述解决方案。

这是我如何在 Windows 10 上更改环境变量的方法:

  • 打开开始搜索,输入“env”,然后选择“编辑系统环境变量”
  • 单击Environment Variables…按钮。
  • 继续系统变量并在变量下找到 PATH,标记它并单击EDIT
  • 在列表的末尾添加路径 - 您只需单击新的或列表末尾的空白字段: C:\Windows\system32 C:\Windows C:\Windows\System32\Wbem
  • 请记住也有 npm 路径: C:\Program Files\nodejs C:\Users{yourName}\AppData\Roaming\npm
  • 根据需要设置环境变量。
  • 新建按钮添加了一个额外的变量。...
  • 通过选择关闭所有对话框OK您的更改已保存!
  • 重新启动计算机。

现在它应该可以工作了!

我花了两天时间才解决这个问题。

由于 react-scripts 的不兼容问题,请尝试安装不同的版本,例如:

这对我有用:

npm install --save react-scripts@2.1.8

正如他们声称的那样,这个确实对许多人有用:

npm install --save react-scripts@3.3.0

经过艰苦的斗争,它应该像对我一样起作用。

有时在 Windows 上,如果 npm 缓存不知何故搞砸了,我会随机收到相同的错误消息。

尝试运行

npm cache clean --force

删除node_modules文件夹和package.lock.json文件,然后运行npm install