尝试运行 npm start 命令时出错

Error when trying to rum npm start command

我正在尝试创建一个反应项目,我有 运行 npx create-react-app my-app 然后 cd my-app 但是当我有 npm start 已显示此错误:

> my-app@0.1.0 start C:\Users\USER\my-app
> react-scripts start

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

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

Error: spawn powershell 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 powershell',
  path: 'powershell',
  spawnargs: [
    '-NoProfile',
    '-NonInteractive',
    '–ExecutionPolicy',
    'Bypass',
    '-EncodedCommand',
    'UwB0AGEAcgB0ACAAIgBgACIAaAB0AHQAcAA6AC8ALwBsAG8AYwBhAGwAaABvAHMAdAA6ADMAMAAwADAAYAAiACIA'
  ]
}
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\HAMOOD\AppData\Roaming\npm-cache\_logs20-08-23T13_09_04_330Z-debug.log

我曾尝试删除 node_modules 然后我又安装了它,但同样的问题仍然存在

我找到了一些可能的解决方案。似乎您正在使用 powershell,首先检查您的 PATH 中是否有它作为 %SystemRoot%/system32/WindowsPowerShell/v1.0。之后以管理员身份打开 powershell 并写入

Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force

如果 powershell 出现问题,它应该会有所帮助。或者,当 cra dev 服务器试图打开网络浏览器时,有时它可能是一个错误。 github 上有人说这对他有帮助:

This error stems from the fact that CRA tries to open your browser: In order to skip opening the browser, add BROWSER=none to your .env or .env.local file

另一种方法是使用这些命令为项目降级反应脚本

npm install react-scripts@2.1.8
npm start

转到 > 控制 Panel\System 和 Security\System\Advance 系统 setting\Enviroment 变量并设置系统变量路径 C:\Windows\System32\ 变量并重新启动系统。

1- 首先你需要编辑系统环境变量并找到你有 Windows Poweshell 的地方所以在“系统环境变量路径”中添加新的

2- 然后以管理员身份打开 Window PoweShell 并写入以下两个命令:

:) Upgrade Window PowerShell because your nodejs and npm is updated but powershell is not updated so its making conflict with your npm start or expo start,so you should need to upgrade windows powershell

然后 运行 Cmd 和 运行 你的项目 npm start 或 expo start 很快就会起作用!

祝你好运!