无法创建React Native项目?安装CLI仍提示命令未找到
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 -gin 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 $PATHand 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~/.bashrcif you’re using a different shell setup). - Add this line at the bottom:
export PATH="$PATH:/usr/local/bin"(replace/usr/local/binwith the output ofnpm bin -gif it’s different). - Save and exit (Ctrl+O, then Enter, then Ctrl+X), then run
source ~/.bash_profileto apply the changes immediately.
- Open your bash configuration file with
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_profileagain and add:export PATH=~/.npm-global/bin:$PATH - Reload the config with
source ~/.bash_profile
- Open
- 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 --versionto 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:
内容的提问来源于stack exchange,提问作者Sureshkumar M

