package.json
As in any NodeJS application, the dependencies are managed by
the package.json file in the root of your project.
After an initial setup, this file contains the a list of the installed packages as well as some runtime specifications:
{
"name": "new-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^13.0.0",
"@testing-library/user-event": "^13.2.1",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
This file outlines all the settings for the React app. Each of the attributes in the file has its importance in some way or the other. Where some of these are basic and clear from their names, some are not.
nameis the name of your app, which you give while executingcreate-react-app <name-of-application>. You can give any name of your choice to the app, the only condition being is that it should be in lowercase. It may also contain hyphens and underscores.versionis the current version of your app. The version field must be of the formx.x.x. By default, create-react-app initializes it as0.1.0"private": trueis one of the most crucial attributes. The use is that if you set private as true in yourpackage.json, thennpmwill refuse to publish it within npm ecosystem. This is a way to prevent the accidental publication of private repositories.dependenciescontains all the required node modules and versions required for the application in production. In the snapshot above, it contains three dependencies, which allows us to usereact,react-domandreact-scriptsin our JavaScript.react-scriptsprovide a set of useful development scripts for working with React.
In the listing above the react version is specified as
^18.1.0, which means that npm will install the most
recent major version matching 18.x.x.
In contrast if you see something like ~5.14.1 in
package.json, it means that it will install the most
recent minor version matching 5.14.x.
Adding libraries/plugins/packages
You can add 3rd party libraries, plugins and packages (which are registered on npmjs.com) to your React application by issuing the following command(s):
- npm
- yarn
npm install <package-name> [--save, --save-dev, --global]
yarn add <package-name> [--save, --save-dev, --global]
--saveadds the package topackage.json(it's the default so it can be omitted)--save-devadds the package ONLY to your development dependencies (like test or debug tools)--globalwill install the package GLOBALLY on your computer, so it can be access everywhere (not recommended).
Scripts
The scripts-part in package.json contains the commands we
can issue to control our application. These commands can be run
with either yarn or npm, although the syntax is fairly
the same, npm requires you add the run subcommand to it:
npm run [COMMAND]
# or
yarn [COMMAND]
Let's take a look at the commands:
start: Fires up a development (web)server listening on port 3000 (default). The server auto-refreshes the page when a change is detected.build: Creates a production ready compiled (transpiled) site in the./buildfolder.test: Runs test (jest) script (if present)eject: Decouples all packages React is using under the hood (in the./node_modulesdirectory) and applies them topackage.json: one way operation!!
You will be using both the start and build frequently. I have never found any use
for the eject command.