[ReactJS] : Install packages In React App

What is package?

A package is a file or directory that is described by a package.json file. A package must contain a package.json file in order to be published to the npm registry. Package is something like module or directory which helps to extends or implements new functionality in our React App.

So before installing new packages we need to Create a React App. Refer https://www.osseed.com/blog/reactjs-create-simple-react-app

There are two types of packages which can be unscoped to a specific user or organisation, and scoped packages can be private or public.

1) Installing an unscoped package
  Unscoped packages are always public, which means they can be searched for, downloaded, and installed by anyone. To install a public package, on the command line, go to react app directory and run

  If you use npm, run:
  npm install <package_name>

  If you use Yarn, run:
  yarn add <package_name>

2) Installed a scoped public package
  Scoped public packages can be downloaded and installed by anyone, as long as the scope name is referenced during installation

  If you use npm, run:
  npm install @scope/package_name

  If you use Yarn, run:
  yarn add @scope/package_name

  Private packages can only be downloaded and installed by those who have been granted read access to the package. Since private packages are always scoped, you must reference the scope name during installation

  If you use npm, run:
  npm install @scope/private_package_name

  If you use Yarn, run:
  yarn add @scope/private_package_name
  
Sometime it creates the permission issues while installing packages so in that case just use sudo before the npm/yarn command.

  If you use npm, run:
  sudo npm install <package_name>

  If you use Yarn, run:
  sudo yarn add <package_name>
  
Once installed it will create a node_modules directory under your react app directory if one doesn’t exist and it will download package under that directory.

For confirmation npm install or yarn add is worked perfectly fine, in your react app directory check that node_modules directory is present and it contains the directory for package you have installed.

  ls node_modules

If there is no package.json file in the local directory, the latest version of the package is installed. If its there and package is already defined and not present in node_modules directory then it will download the same version which is defined package.json file.