© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2021
D. B. Duldulao, R. J. L. CabagnotPractical Enterprise Reacthttps://doi.org/10.1007/978-1-4842-6975-6_2

2. Getting Started with the Node Package Manager

Devlin Basilan Duldulao1   and Ruby Jane Leyva Cabagnot1
(1)
Oslo, Norway
 

Before we can get React up and running, we need an installer to download and manage our JavaScript software packages. One of the most popular package managers is the Node Package Manager (NPM). Another one is Yarn, a newer package manager that supposedly drew a lot of inspiration from NPM. For our project, we opted to use NPM instead of Yarn. You can use Yarn if you prefer.

Our project will be using NPM, so this chapter will go through some of the most pertinent NPM commands and shortcuts, including semantic versioning and NPM scripts. We will just briefly discuss Yarn and go through some commonly used Yarn commands.

Don’t worry about following the commands here for now because we would be doing it step-by-step in the succeeding chapters once we begin building our project.

Node Package Manager Overview

Before we get started, let’s review what NPM is and how we use it in our app. NPM is similar to other package managers, such as RubyGems in Ruby on Rails or PIP in Python.

Released in 2010, NPM is JavaScript’s package library manager, usually preinstalled with Node.js, an environment for building server-side applications. If you don’t have Node.js installed yet, go to their website at www.nodejs.org (Figure 2-1).
../images/506956_1_En_2_Chapter/506956_1_En_2_Fig1_HTML.jpg
Figure 2-1

Node’s website

Make sure to install the Long Term Support (LTS) version because it is more stable than the current version. If you already have Node installed, you can check the version:
$ node –version

Speaking of Node versioning, let me introduce you – if you’re not familiar with it yet – to Node Version Manager, or nvm for short.

Node Version Manager

Node Version Manager (NVM) is a tool for managing different versions of the Node.js runtime.

You can easily downgrade or upgrade the Node.js version, which will come in handy if you face a legacy application or a JavaScript library that is only compatible with a specific range of Node.js versions.

Install nvm with a single command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
#or
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.0/install.sh | bash

For a complete installation of NVM in Mac and Linux, go here https://github.com/nvm-sh/nvm .

Unfortunately, nvm does not support Windows, but it’s possible to do it in Windows Subsystem for Linux (WSL) depending on the WSL version.

To download and install NVM in Windows, go here https://github.com/coreybutler/nvm-windows.

After installing NVM, you can install a specific version of Node:
$ nvm install 14
$ nvm use 14
The preceding commands will install and use the latest minor version of Node v14. Now, if there’s a JavaScript project or library that only runs in Node v12, you can easily switch to that version:
$ nvm install 12
$ nvm use 12

The preceding commands will install and use the latest minor version of Node v12.

Another indispensable resource for any JavaScript developer is the npmjs website, where we can search for libraries or frameworks that we need to install for our project.

Installing Packages

You can use NPM to install just about any library or framework available in the NPM registry. You can head to www.npmjs.com to see more. If you already have an older version, you can update as follows.

For Mac and Linux users:
$ npm install -g npm@latest

Node Packaged Modules

Packages are either installed in local or global mode. Global mode packages are available from the command-line interface (CLI), while packages in local mode are installed in a node_modules folder of the parent working file.

Set up a new or an existing NPM package:
$ npm init
$ npm init –-y

Fill in the blanks or just press Enter to accept the default values or add the flag –-y to quickly generate a package.json file.

If you want to clone someone else’s code, run the following command in the project root:
$ npm install or npm i

This will automatically fetch all the declared packages needed for you to run the app. The declared packages are located in the package.json file.

Package.json and Package-lock.json

Package.json is an important manifest file when using NPM and Node applications. It contains all of your app info, particularly the dependencies or modules that your app requires to run correctly. This manifest file is also the first thing that many developers would look at to run their local versions of your project.

Package-lock.json is a replica, versioned dependency tree of your package.json file.

A package.json file as shown in Listing 2-1 was automatically created when we did the npm install command in the terminal. This is just a sample object structure of a package.json file, including the dependencies and devDependencies. We will start installing the packages and libraries for our app in Chapter 4.
{ "name": "npmproject",
   "version": "1.0.0",
   "private"
   "description": "NPM commands",
   "main": "index.js",
   "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject",
   "backend": "json-server --watch db.json --port 5000 -- delay=500",
   "start:fullstack": "concurrently "npm run backend" "npm run start""
 },
    "author": "Devlin Duldulao",
    "license": "MIT",
    "dependencies": {
    "react": "^16.8.6",
               "react-dom": "^16.8.6",
},
"devDependencies": {
    "husky": "^4.3.0",
    "json-server": "^0.16.2",
    "lint-staged": "^10.4.0",
    "mobx-react-devtools": "^6.1.1",
    "prettier": "^2.1.2"
  },
 }
Listing 2-1

A Sample of a package.json File

While package.json is used for dependencies such as project properties, author, version, description, scripts, license, etc., package-lock.json is likewise automatically created to lock dependencies to a specific version number.

As shown in Listing 2-1, the scripts object is the first thing I always read in an existing project because the scripts tell us what commands to run in order to run the project or build the project. The scripts object also helps us run shorter commands through its key and value pair. Another cool thing you can do in your scripts is to customize it.

Let’s say you want to simultaneously use the scripts ‘npm run backend’ and ‘npm run start’. Then you could just add a shortcut in your script such as ‘npm start:fullstack’ and run it:
"start:fullstack": "concurrently "npm run backend" "npm run start""
If you want to try for yourself the start:fullstack command, you can npm install concurrently. Concurrently is a very convenient tool that lets us run multiple commands at the same time:
$ npm i concurrently

The Dependencies, which is in our example just initially contains the bare minimum such as react and react-dom, are libraries that we require for our application in runtime. But take note also of the devDependencies object. The devDependencies object contains JavaScript libraries that you want to be added only during local development and testing because you don’t need them in production. Some good examples include Gulp, Prettier, and ESLint.

Semantic Versioning or Semver

There are several types of versioning, but semantic versioning or semver is one of the most popular.

There are three version numbers to consider in so-called semantic versioning. On its official website, www.semver.org/, we can see the following summary:

Given a version number MAJOR.MINOR.PATCH, increment the:

MAJOR version when you make incompatible API changes,

MINOR version when you add functionality in a backward-compatible manner, and

PATCH version when you make backward-compatible bug fixes.

Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.

An example of semantic versioning is “^3.2.1”.

The first number (3) is the major version, (2) is the minor version, and (1) is the patch version.

The caret ^ tells NPM that we will take in the major version of that scheme; the minor and patch versions can vary. Another way to write this is, for example, 2.x, with 2 being the major version.

In some cases, you will see the tilde ~ (i.e., “~3.2.1”). This particular versioning can be read as “we will take the major version to be 3 and the minor version to be 2, but the patch version can be any number.” You could also write this as 3.2.x.

In case you need the specific numbers for all three versions, simply remove the caret or tilde character.

Yarn

Yarn is a new package manager for JavaScript. Yarn, released in 2016 by Facebook, was designed to supposedly address some performance and security issues hounding NPM (at that time!). As stated earlier, we will not be using Yarn, but you can use it instead of NPM. In the following, we highlight some of the similarities and differences in syntax between the two.

Tables 2-1 and 2-2 highlight the similarities and differences between the two package managers.
Table 2-1

Common Commands Between NPM and Yarn

Command

NPM

Yarn

To initialize a project

npm init

yarn init

To set up the defaults

npm init –y

yarn init -y

To check if any package is outdated

npm outdated

yarn outdated

To clear local cache

npm cache clean

yarn cache clean

To run a script

npm run build

yarn run

To see a list of installed dependencies

npm list

yarn list

Table 2-2

Different Commands Between NPM and Yarn

Command

NPM

Yarn

To install dependencies

npm install

yarn install

To install packages

npm install[package-name]

yarn add [package-name]

To uninstall packages

npm uninstall [package-name]

yarn remove [package-name]

To install packages globally

npm install –global [package-name]

yarn global add [package-name]

To uninstall packages globally

npm uninstall –global [package-name]

yarn global remove [package-name]

To update packages * for updating minor and patch releases only

npm update [package-name]

yarn upgrade [package-name]

To install only regular dependencies

npm install --production

yarn --production

To show only the top-level dependencies

npm list -g --depth 0

yarn list --depth=0

To install and save packages in devDependencies

npm install --save-dev [package-name

yarn add [package-name] -D

Tips in using NPM:
  1. 1.

    To generate a package.json file quickly, use npm init –-y.

     
  2. 2.

    Add private: true to package.json to prevent accidental publishing of any private repo.

     
  3. 3.

    Add packages (i.e., transpiling code or running tests) used for development purposes in your devDependencies.

     
  4. 4.

    Don’t delete the package.json, but you can delete the package-lock.json before committing it.

     
  5. 5.

    If you encounter yarn.lock and want to use NPM instead, just delete the yarn.lock and do an npm install to automatically create a package-lock.json in your application.

     
  6. 6.

    You need to run npm install after cloning a project from a Git repository.

     
  7. 7.

    It is not recommended to push node_modules to your source control repo such as Git.

     

Summary

In this chapter, we have discussed how to get started with NPM including the various key commands that you need to understand going forward. We’ve also looked into using nvm to easily switch from one Node version to another. In the next chapter, we will talk about the various React components and how we use them in our application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.2.122