为什么我的npm安装在Mac上为React Native失败?

时间:2018-02-08 17:16:56

标签: macos react-native npm npm-install

React Native的新功能我确保从here安装node.js并在安装后告知:

Node.js v9.5.0 to /usr/local/bin/node
npm v5.6.0 to /usr/local/bin/npm

我可以验证节点版本:

node --version: v9.5.0

我为npm install

运行了文档getting started
npm install -g create-react-native-app

并且还引用了building projects并使用brew安装了watchman:

watchman 4.9.0
在终端中

运行:npm install -g react-native-cli和终端:npm start我得到:

grim ~/react-native/react-native npm start

> react-native@1000.0.0 start /Users/grim/React-Native/react-native
> /usr/bin/env bash -c './scripts/packager.sh "$@" || true' --

module.js:557
    throw err;
    ^

Error: Cannot find module 'graceful-fs'
    at Function.Module._resolveFilename (module.js:555:15)
    at Function.Module._load (module.js:482:25)
    at Module.require (module.js:604:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/grim/React-Native/react-native/local-cli/cli.js:12:1)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
grim ~/react-native/react-native 

经过研究,我发现NPM, cannot find 'graceful-fs', no matter what I do并在终端跑了:

sudo npm install -g graceful-fs

并获得:

+ graceful-fs@4.1.11
added 1 package in 0.678s

但是当我运行npm install时,我得到了:

npm ERR! code EUNSUPPORTEDPROTOCOL
npm ERR! Unsupported URL Type "npm:": npm:babylon@^7.0.0-beta
npm WARN deprecated gulp-util@3.0.8: gulp-util is deprecated - replace it, following the guidelines at https://medium.com/gulpjs/gulp-util-ca3b1f9f9ac5

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/grim/.npm/_logs/2018-02-08T17_08_50_619Z-debug.log

并在文件日志中显示:

116 error code EUNSUPPORTEDPROTOCOL
117 error Unsupported URL Type "npm:": npm:babylon@^7.0.0-beta

所以我去The Problem with gulp-util但是当我这样做时:

npm ls gulp-util

我得到└── (empty)

进一步的研究我发现Why is my npm install not working?但是引发npm ERR!我做错了什么?如何在运行Seirra的Mac上成功安装npm

导致这种情况的原因是试图从react native repo测试RNTester并告诉我:

git clone https://github.com/facebook/react-native.git
cd react-native
npm install

修改

根据已经尝试过的建议答案brew install node,系统会发出警告node 9.5.0 is already installed,因此尝试brew link node然后提示:

grim ~/react-native/react-native brew link node
Linking /usr/local/Cellar/node/9.5.0... 
Error: Could not symlink bin/node
Target /usr/local/bin/node
already exists. You may want to remove it:
  rm '/usr/local/bin/node'

To force the link and overwrite all conflicting files:
  brew link --overwrite node

To list all files that would be deleted:
  brew link --overwrite --dry-run node

所以跑了brew link --overwrite node

修改

浏览Users/grim下的文件我发现一个名为node_modules的模块目录可能位于错误的位置?

修改

经过进一步研究后,我找到Fixing npm On Mac OS X for Homebrew Users并跑了:

rm -rf /usr/local/lib/node_modules
brew uninstall node
brew install node --without-npm
echo prefix=~/.npm-packages >> ~/.npmrc
curl -L https://www.npmjs.com/install.sh | sh

使用nano修改了我的~/.bash_profile

export PATH="$HOME/.npm-packages/bin:$PATH"

运行npm install并收到了一个丢失的package.json文件,因此在终端中npm init并回答了问题。回来并指向克隆的回购,当我运行npm install时:

npm ERR! code EUNSUPPORTEDPROTOCOL
npm ERR! Unsupported URL Type "npm:": npm:babylon@^7.0.0-beta
npm WARN deprecated gulp-util@3.0.8: gulp-util is deprecated - replace it, following the guidelines at https://medium.com/gulpjs/gulp-util-ca3b1f9f9ac5

但如果我使用react-native init AwesomeProjectnpm install构建新项目,我会:

up to date in 0.416s

我可以指向该项目并执行react-native run-ios并且它有效,但react-native run-android必须在build.bundleclasspath 'com.android.tools.build:gradle:2.2.3'classpath "com.android.tools.build:gradle:2.1.0"进行调整

2 个答案:

答案 0 :(得分:1)

使用Homebrew for Mac作为包管理器;

https://brew.sh/

然后你可以运行:brew install node

此处还有许多其他实用程序。

答案 1 :(得分:0)

create-react-native app不适用于npm 5.

我在Windows上遇到了同样的问题并将我的npm降级为4你可以使用npm i -g npm @ 4来降级。