Skip to content

Fixing "TypeError: cli.init is not a function" in React Native

When trying to create a new React Native project with npx react-native init appName, you might encounter the error:

javascript
TypeError: cli.init is not a function
    at run (/opt/homebrew/lib/node_modules/react-native-cli/index.js:302:7)
    at createProject (/opt/homebrew/lib/node_modules/react-native-cli/index.js:249:3)
    // ... rest of stack trace

This error typically occurs due to outdated global installations or version conflicts with React Native's CLI tools.

Root Cause

The issue stems from having outdated global React Native CLI packages that conflict with the current React Native initialization process. As of React Native 0.69+, the CLI structure changed, making older global installations incompatible.

The most reliable approach is to use the community CLI directly without global installations:

bash
npx @react-native-community/cli@latest init AwesomeProject

This command:

  • Uses the latest version of the community-maintained CLI
  • Avoids conflicts with any globally installed packages
  • Is the officially recommended approach by React Native

Solution 2: Clean Global Installations and Use NPX

If you prefer the traditional command, first clean up any global installations:

bash
# Uninstall global packages
npm uninstall -g react-native
npm uninstall -g react-native-cli
npm uninstall -g @react-native-community/cli

# Verify no React Native packages remain
npm -g list

# Create new project
npx react-native@latest init AwesomeProject
bash
# Uninstall global packages
yarn global remove react-native
yarn global remove react-native-cli
yarn global remove @react-native-community/cli

# Verify no React Native packages remain
yarn global list

# Create new project
npx react-native@latest init AwesomeProject

Solution 3: Specific Version Initialization (If Needed)

If you encounter issues with the latest version, you can target a specific React Native version:

bash
npx react-native init ProjectName --version 0.68.2

After creating the project, you can upgrade to newer versions following React Native's upgrade guide.

Preventing Future Issues

WARNING

Avoid installing React Native packages globally. The npx command automatically uses the latest version without polluting your global namespace.

React Native's documentation explicitly recommends against global installations:

"If you previously installed a global react-native-cli package, please remove it as it may cause unexpected issues."

Troubleshooting Additional Problems

If you still encounter issues after trying the solutions above:

  1. Check for Homebrew installations: If you installed React Native via Homebrew, remove it:

    bash
    brew remove react-native-cli
  2. Clear npm cache:

    bash
    npm cache clean --force
  3. Remove node_modules in your home directory: Sometimes leftover node_modules in your user directory can cause conflicts.

  4. Verify Node.js version: Ensure you're using a Node.js version compatible with your React Native version.

Best Practices

  • Always use npx instead of global installations
  • Regularly update your Node.js and npm/yarn versions
  • Consult the official React Native documentation for the most current setup instructions
  • Consider using a Node version manager (like nvm) to manage different Node.js versions for different projects

By following these solutions and best practices, you should be able to successfully create new React Native projects without encountering the "cli.init is not a function" error.