Tired of reinventing the wheel?
I'm building a React Native component library - built with Tailwind CSS - designed to help developers build awesome apps in record time.
If you find yourself getting a “command not found” error when you try to initialise a React Native project (or run any other React Native CLI command), you are likely using an outdated method of running the React Native CLI. In this article I will explain the problem and tell you how to fix it so you can get up and running again.
Why am I seeing “command not found: react-native”?
If you are getting this error, it means that either you do not have the CLI installed on your machine, or you do not have it configured properly.
In the past I would have spent the rest of this article telling you how to install the CLI the old fashion way by running npm install -g react-native-cli, and ensuring it’s in your machines path.
Thankfully, npm now provides a much simpler option than going through all that hassle. Instead, we can use NPX to run the latest CLI version on demand without having to configure anything.
What is NPX?
NPX is a tool included in NPM (from version 5.2) that allows you to run executables that you would normally install via NPM directly. The major advantage of using npx is that it requires no configuration, and it will always run the latest version of the package that you’re executing.
This makes it ideal for something like the React Native CLI, which would traditionally have been installed globally and become out of date relatively quickly unless you remember to update it.
How to check that you have NPX available
To check that you have npx available, simply run the following command in your terminal:
This should return the directory path to your npx install - if so, you’re ready to go! If not, then you can install npx by running:
How to use the npx version of React Native CLI
Now that you’ve confirmed that you have access to npx, you should now be able to initialise a React Native project by running the following command in your terminal:
It will ask you for permission to install the react-native package - simply enter y to proceed and your React Native project should be bootstrapped!
You can run any command via the npx version that you would on the npm version - for example to start the metro bundler, simply run the following in your project root: