npm 创建React Native项目时出现命令行错误

pn9klfpd  于 2022-11-14  发布在  React
关注(0)|答案(5)|浏览(237)

我刚开始使用react-native项目,并试图设置一个演示项目来学习react-native。但是,我无法通过命令行创建一个项目。
我在安装了必备库之后使用了以下命令来创建react-native项目:

react-native init AwesomeProj

出现以下错误:

This will walk you through creating a new React Native project in /Users/ketanmalhotra/Documents/Freelance/demo-react/AwesomeProject
Installing react-native...
Consider installing yarn to make this faster: https://yarnpkg.com
npm ERR! Darwin 16.3.0
npm ERR! argv "/usr/local/Cellar/node/6.3.0/bin/node" "/usr/local/bin/npm" "install" "--save" "--save-exact" "react-native"
npm ERR! node v6.3.0
npm ERR! npm  v3.10.3
npm ERR! code ETIMEDOUT
npm ERR! errno ETIMEDOUT
npm ERR! syscall connect

npm ERR! network connect ETIMEDOUT 151.101.36.162:443
npm ERR! network This is most likely not a problem with npm itself
npm ERR! network and is related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network 
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/ketanmalhotra/Documents/Freelance/demo-react/AwesomeProject/npm-debug.log
{ Error: Command failed: npm install --save --save-exact react-native
    at checkExecSyncError (child_process.js:475:13)
    at execSync (child_process.js:515:13)
    at run (/usr/local/lib/node_modules/react-native-cli/index.js:294:5)
    at createProject (/usr/local/lib/node_modules/react-native-cli/index.js:249:3)
    at /usr/local/lib/node_modules/react-native-cli/index.js:217:7
    at /usr/local/lib/node_modules/react-native-cli/node_modules/prompt/lib/prompt.js:316:32
    at /usr/local/lib/node_modules/react-native-cli/node_modules/async/lib/async.js:142:25
    at assembler (/usr/local/lib/node_modules/react-native-cli/node_modules/prompt/lib/prompt.js:313:9)
    at /usr/local/lib/node_modules/react-native-cli/node_modules/prompt/lib/prompt.js:322:32
    at /usr/local/lib/node_modules/react-native-cli/node_modules/prompt/lib/prompt.js:597:5
  error: null,
  cmd: 'npm install --save --save-exact react-native',
  file: '/bin/sh',
  args: 
   [ '/bin/sh',
     '-c',
     'npm install --save --save-exact react-native' ],
  options: 
   { stdio: [ [Object], [Object], [Object] ],
     shell: true,
     file: '/bin/sh',
     args: 
      [ '/bin/sh',
        '-c',
        'npm install --save --save-exact react-native' ],
     envPairs: 
      [ 'TERM_PROGRAM=Apple_Terminal',
        'SHELL=/bin/bash',
        'TERM=xterm-256color',
        'TMPDIR=/var/folders/55/sqks2tv55flg67c1bbb2d9kh0000gn/T/',
        'Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.qiouajydN4/Render',
        'TERM_PROGRAM_VERSION=388',
        'OLDPWD=/Users/ketanmalhotra',
        'TERM_SESSION_ID=473A481F-186B-4E05-B4D4-7A319C0F4D9F',
        'USER=ketanmalhotra',
        'SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.rZIxxhyFU9/Listeners',
        '__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0',
        'PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin',
        'PWD=/Users/ketanmalhotra/Documents/Freelance/demo-react',
        'XPC_FLAGS=0x0',
        'XPC_SERVICE_NAME=0',
        'SHLVL=1',
        'HOME=/Users/ketanmalhotra',
        'LOGNAME=ketanmalhotra',
        'LC_CTYPE=UTF-8',
        'DISPLAY=/private/tmp/com.apple.launchd.3GdGOWMzei/org.macosforge.xquartz:0',
        '_=/usr/local/bin/react-native' ] },
  envPairs: 
   [ 'TERM_PROGRAM=Apple_Terminal',
     'SHELL=/bin/bash',
     'TERM=xterm-256color',
     'TMPDIR=/var/folders/55/sqks2tv55flg67c1bbb2d9kh0000gn/T/',
     'Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.qiouajydN4/Render',
     'TERM_PROGRAM_VERSION=388',
     'OLDPWD=/Users/ketanmalhotra',
     'TERM_SESSION_ID=473A481F-186B-4E05-B4D4-7A319C0F4D9F',
     'USER=ketanmalhotra',
     'SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.rZIxxhyFU9/Listeners',
     '__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0',
     'PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin',
     'PWD=/Users/ketanmalhotra/Documents/Freelance/demo-react',
     'XPC_FLAGS=0x0',
     'XPC_SERVICE_NAME=0',
     'SHLVL=1',
     'HOME=/Users/ketanmalhotra',
     'LOGNAME=ketanmalhotra',
     'LC_CTYPE=UTF-8',
     'DISPLAY=/private/tmp/com.apple.launchd.3GdGOWMzei/org.macosforge.xquartz:0',
     '_=/usr/local/bin/react-native' ],
  stderr: null,
  stdout: null,
  pid: 79752,
  output: [ null, null, null ],
  signal: null,
  status: 1 }
Command `npm install --save --save-exact react-native` failed.

我试图找到解决这个问题的方法,但找不到任何有效的方法。有人知道如何解决这个问题吗?
附言:我是在Mac上做的(如果这有什么不同的话)

ru9i0ody

ru9i0ody1#

我检查了互联网问题,也ping了registry.npmjs.org,一切都工作正常。为了解决这个问题,我使用以下命令安装了yarn:

brew install yarn

它似乎已经解决了这个问题。如果有人在开始使用react-native时遇到同样的问题,我就把它放在这里。

zqry0prt

zqry0prt2#

npm cache clean --force 
brew install yarn
npm install --save --save-exact react-native

或者手动安装Yarn。

cd /opt
wget https://yarnpkg.com/latest.tar.gz
tar zvxf latest.tar.gz

则运行

npm install --save --save-exact react-native
raogr8fs

raogr8fs3#

npm cache clean --force

运行此命令首先为我解决了问题。

cpjpxq1n

cpjpxq1n4#

我通过手动下载并安装Yarn解决了问题
请使用以下命令:

yarn config set "strict-ssl" false -g  
yarn add react-native --exact
j0pj023g

j0pj023g5#

如果您在Windows上运行,请***禁用防火墙***。

相关问题