无法在 Ubuntu 21.10 上创建新的 React 应用程序

Posted

技术标签:

【中文标题】无法在 Ubuntu 21.10 上创建新的 React 应用程序【英文标题】:Cannot create a new React application with create-react-app 【发布时间】:2022-01-17 15:31:46 【问题描述】:

我在创建新的 React 应用程序时遇到了这个问题(在执行npx create-react-app my-app 命令之后):

    ➤ YN0000: ┌ Resolution step
node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

_r [RequestError]: read ECONNRESET
    at ClientRequest.<anonymous> (/home/aldaspan/.yarn/releases/yarn-3.1.1.cjs:154:14361)
    at Object.onceWrapper (node:events:510:26)
    at ClientRequest.emit (node:events:402:35)
    at ClientRequest.o.emit (/home/aldaspan/.yarn/releases/yarn-3.1.1.cjs:149:59453)
    at TLSSocket.socketErrorListener (node:_http_client:447:9)
    at TLSSocket.emit (node:events:390:28)
    at emitErrorNT (node:internal/streams/destroy:164:8)
    at emitErrorCloseNT (node:internal/streams/destroy:129:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
    at TLSWrap.onStreamRead (node:internal/stream_base_commons:211:20) 
  code: 'ECONNRESET',
  timings: 
    start: 1639471914529,
    socket: 1639471914529,
    lookup: 1639471914530,
    connect: 1639471922067,
    secureConnect: undefined,
    upload: undefined,
    response: undefined,
    end: undefined,
    error: 1639471922468,
    abort: undefined,
    phases: 
      wait: 0,
      dns: 1,
      tcp: 7537,
      tls: undefined,
      request: undefined,
      firstByte: undefined,
      download: undefined,
      total: 7939
    
  


Node.js v17.2.0

Aborting installation.
  yarnpkg add --exact react react-dom react-scripts cra-template --cwd /home/aldaspan/galog/react-shop has failed.

Deleting generated file... package.json
Deleting generated file... yarn.lock
Done.

操作系统:Ubuntu 21.10, 节点-v:v17.2.0, nodejs --版本:v12.22.5, npm -v: 8.1.4

我是React新手,找这个问题好几天了,还没找到真正的解决办法。

降低Node(sudo n 16.13.1)版本后提示其他错误:

YN0000: Done with warnings in 5m 4s
node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module 'react-scripts/scripts/init.js'
Require stack:
- /home/aldaspan/react-shop/[eval]
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at [eval]:2:20
    at Script.runInThisContext (node:vm:129:12)
    at Object.runInThisContext (node:vm:305:38)
    at node:internal/process/execution:75:19
    at [eval]-wrapper:6:22
    at evalScript (node:internal/process/execution:74:60) 
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/home/aldaspan/react-shop/[eval]' ]


Aborting installation.
  node  has failed.

Deleting generated file... package.json
Deleting generated file... yarn.lock
Done.

【问题讨论】:

【参考方案1】: 您应该使用推荐的node 版本,目前是16。为此,您可以使用n 模块:
// if you haven't istalled before 
npm install n -g 
//to have the latest stable version
n stable
// if one of them do not pass, may be should be using sudo, like so :
sudo npm install n -g 
sudo n stable 
如果你已经全局安装了create-react-app(你不应该因为我们现在使用npx),你需要像这样删除它:
npm uninstall -g create-react-app
// or with yarn like so :
yarn global remove create-react-app
然后删除您尝试创建的项目并重新开始:
npx create-react-app my-ap

【讨论】:

降低Node(sudo n 16.13.1)版本后提示其他错误。 ***.com/a/63480893/10079978 查看此链接 这个问题终于解决了。 1. 按照@yousoumar的规定,Node版本降为16.13.10。 2. 然后重启电脑。 3.因为我之前有created-react-app通过全局安装npm install -g create-react-app。所以使用npm uninstall -g create-react-app或者卸载包yarn global remove create-react-app来保证npx始终使用最新版本。 4.然后我执行npx create-react-app my-ap命令,成功了。非常感谢! 很高兴它成功了,很抱歉我没有参加@aldaspan 的谈话!我不认为可能是您将create-react-appnode 的其他版本一起全局安装。我将在回复中添加这一点,所以如果其他人过来知道。

以上是关于无法在 Ubuntu 21.10 上创建新的 React 应用程序的主要内容,如果未能解决你的问题,请参考以下文章

ubuntu 21.10以后安装搜狗输入法的方法

启用 Secure Boot 时 rEFInd 无法使用 shim 加载的解决方案

21.9 redis介绍 21.10 redis安装 21.11 redis持久化 21.12 re

Ubuntu21.10下安装使用Armadillo库

Ubuntu21.10下安装使用Armadillo库

Ubuntu21.10下安装TensorFlow及配置GPU支持(cuda11.1+cudnn8.0.5)