![]() |
VOOZH | about |
React Native is a popular framework for building cross-platform mobile applications using JavaScript and React. One of the key tools you'll use when working with React Native is npm (Node Package Manager), which is a package manager for JavaScript and plays an integral role in managing dependencies in your project.
In this article, we'll see the fundamentals of npm in React Native, covering everything from installation to package management.
Once you have Node.js and npm installed, you can create a new React Native project. The recommended way to start a new React Native project is by using npx, which comes preinstalled with npm.
You can create a new React Native project by running the following command in your terminal:
npx react-native init MyNewAppThis command initializes a new React Native project called MyNewApp using the default template. It will create a project structure that includes files such as package.json, App.js, and folders like node_modules, where your npm packages will be installed.
This will create a project like this:
Once the project is created, npm will automatically install all necessary dependencies. The node_modules folder contains all the packages your project relies on, including React and React Native itself.
If for some reason npm does not install your dependencies, you can manually install them by running:
npm installThis command reads the package.json file and installs the dependencies listed under the dependencies and devDependencies sections.
"dependencies": {
"react": "18.3.1",
"react-native": "0.75.4"
}
Several files related to npm are important for managing your React Native project:
Here are some of the most common npm commands you’ll use when working on a React Native project:
The package.json file can define custom scripts that help you automate tasks. For example, if you want to create a script that runs your React Native app on an iOS or Android emulator, you can add the following to your package.json file:
"scripts": {
"start": "react-native start",
"android": "react-native run-android",
"ios": "react-native run-ios"
}
Now, you can simply run npm run android or npm run ios to launch your app on the corresponding platform.
Once you have installed all the dependencies for your project, you can start the development server with the following command:
npm startThis command starts the Metro bundler, which packages your app's JavaScript code for development. The Metro bundler will watch for changes in your code and automatically reload your app in the emulator or on your device whenever you save a file.