首页 > 解决方案 > 在终端中找不到 React-native 命令

问题描述

在尝试创建新的 react native 项目时,我一直遇到“找不到命令”错误。我已经查看了有关此主题的过去问题,他们都说要更改 PATH,这似乎对我不起作用。

我尝试安装和卸载 react-native-cli。我已经检查并更改了 PATH。没有任何效果。

Davids-MBP-2:~ David$ brew -v
Homebrew >=1.7.1 (shallow or no git repository)
Homebrew/homebrew-core (no git repository)
Davids-MBP-2:~ David$ watchman -v
4.9.0
Davids-MBP-2:~ David$ node -v
v11.13.0
Davids-MBP-2:~ David$ npm install -g react-native-cli
/Users/David/.npm-global/bin/react-native -> /Users/David/.npm-global/lib/node_modules/react-native-cli/index.js
+ react-native-cli@2.0.1
updated 1 package in 1.392s
Davids-MBP-2:~ David$ react-native init hello
-bash: react-native: command not found
Davids-MBP-2:~ David$ 

我安装了 node、homebrew、watchman,最后安装了 react-native。前三个工作,但反应没有。

标签: macosreact-nativereact-native-cli

解决方案


我将首先描述我系统上的设置。

首先检查brew install node您的node_modules.

$ npm root -g
/usr/local/lib/node_modules

接下来,检查安装的脚本/命令是否在相应的bin文件夹中:

$ ls -l /usr/local/bin | grep react-native
... react-native -> ../lib/node_modules/react-native-cli/index.js

接下来,确保bin文件夹位于您的$PATH.

$ echo $PATH
...:/usr/local/bin:...

最后,通过将其添加到您的~/.bash_profile中来保留该bin文件夹的路径。PATH

$ cat ~/.bash_profile
...
export PATH=$PATH:/usr/local/bin
...
$ source ~/.bash_profile

(我不确定您的终端环境是否需要您source ~/.bash_profile每次打开新的终端会话时都这样做。)


现在,根据您的评论,您的node_modules似乎安装在非标准文件夹中。

这是 npm root -g 的输出:/Users/David/.npm-global/lib/node_modules

尝试执行我上面提到的相同步骤:

  1. 查找相应的bin文件夹(可能在/Users/David/.npm-global/bin/中?)
  2. bin文件夹添加到您的PATH
  3. 通过将修改后PATH的内容放入~/.bash_profile中来保持修改。

如果你找不到bin/react-native,试试这些:

$ find /Users/David/ -name "react-native" -type l
$ find /usr/local/bin -name "react-native" -type l

这些命令可能需要很长时间,但它应该返回如下内容:

$ find /usr/local/bin -name "react-native" -type l 
/usr/local/bin/react-native

找到react-nativebin 的路径后,只需将其添加PATH并保存即可。


推荐阅读