macos - 在终端中找不到 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。前三个工作,但反应没有。
解决方案
我将首先描述我系统上的设置。
首先检查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
尝试执行我上面提到的相同步骤:
- 查找相应的bin文件夹(可能在/Users/David/.npm-global/bin/中?)
- 将bin文件夹添加到您的
PATH
- 通过将修改后
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-native
bin 的路径后,只需将其添加PATH
并保存即可。
推荐阅读
- mysql - 带有AND条件的MYSQL多对多选择
- reactjs - reactjs - 如何为reactjs中的所有子页面创建通用页眉和页脚?
- assembly - 组装 JMP 和 RET
- php - PHP:如何用单引号和双引号分配大字符串
- gem5 - 自定义内核之间的 L2 缓存共享
- javascript - 缓慢的第三方 API 阻塞 Express 服务器
- python - 如何通过 socks 代理访问 Azure Blob 存储?
- linear-programming - 如何解决 mathprog 中的域外错误?
- python-3.x - 在 Dash/Plotly 中显示 PIL 图像
- javascript - 在反应路由器中切换页面时未加载js文件