You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法创建React Native项目?安装CLI仍提示命令未找到

Fixing "-bash: react-native: command not found" Error

Hey there, let’s get that frustrating react-native: command not found error sorted out! I’ve run into this exact issue multiple times, so here’s a step-by-step breakdown of what’s going wrong and how to fix it:

1. First, Check Your System’s PATH Environment Variable

React Native CLI gets installed as a global Node.js module, which means your terminal needs to know where to find its executable file. Here’s how to verify and fix this:

  • Run npm root -g in your terminal to find where your global Node modules are stored (usually something like /usr/local/lib/node_modules).
  • Next, check if the bin directory of that global module path is in your system’s PATH. Run echo $PATH and look for a path like /usr/local/bin (this is where npm typically links global command-line tools).
  • If you don’t see that bin path in your output, you need to add it:
    • Open your bash configuration file with nano ~/.bash_profile (or ~/.bashrc if you’re using a different shell setup).
    • Add this line at the bottom: export PATH="$PATH:/usr/local/bin" (replace /usr/local/bin with the output of npm bin -g if it’s different).
    • Save and exit (Ctrl+O, then Enter, then Ctrl+X), then run source ~/.bash_profile to apply the changes immediately.

2. Reinstall React Native CLI the Right Way

Sometimes the initial installation gets corrupted, or you might have installed the old deprecated CLI. Let’s do a clean install:

  • First, uninstall any existing version: npm uninstall -g react-native-cli
  • Install the official, maintained CLI package: npm install -g @react-native-community/cli
  • After installation, test if the command works: react-native --version — you should see the CLI version number pop up if it worked.

3. Fix Permissions for Global npm Modules

If you’re hitting permission errors during installation (or the CLI just won’t show up), it’s likely because npm is trying to install global modules in a system directory you don’t have write access to. Here’s how to fix that:

  • Create a dedicated global npm directory in your user folder: mkdir ~/.npm-global
  • Tell npm to use this directory for global installs: npm config set prefix '~/.npm-global'
  • Add this new directory’s bin folder to your PATH:
    • Open ~/.bash_profile again and add: export PATH=~/.npm-global/bin:$PATH
    • Reload the config with source ~/.bash_profile
  • Now reinstall the React Native CLI: npm install -g @react-native-community/cli

4. Verify Everything Works

Once you’ve gone through the steps above, test it out:

  • Run react-native --version to confirm the command is recognized.
  • Try creating a test project with react-native init MyTestProject — this should work without the "command not found" error now.

Terminal Operation & Error Screenshot:
Terminal showing "react-native: command not found" error

内容的提问来源于stack exchange,提问作者Sureshkumar M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:38:04