How to Install a Package using npm tool

Posted on

This is an article where the main focus is to show how to install a package or module using ‘npm’ tool. The package or the module for further installation exist as the additional parameter of the command installation. Basically, the following is the command pattern for installing a new module or package :

npm install --save module_name_or_package_name

The following is the example of the command using the above command pattern :

C:\node\chat-app\client>npm install --save cors nodemo express socket.io
npm WARN tsutils@3.17.1 requires a peer of typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.1.2 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.12 (node_modules\webpack-dev-server\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.12: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.12 (node_modules\watchpack\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.12: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.12 (node_modules\jest-haste-map\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.12: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

+ cors@2.8.5
+ socket.io@2.3.0
+ express@4.17.1
+ nodemo@1.0.0
added 40 packages from 29 contributors, updated 1 package and audited 931607 packages in 87.139s

58 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities


C:\node\app>

The installation for adding a new package or module with the name of ‘cors, nodemo, express and socket.io’ is a success. At first, the package.json file only have the following content :

{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}

But after the execution of the above command for installing packages, the content of the file with the name of ‘package.json’ is different. There is another content where the content of it represent the package or module name as follows :

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "nodemon": "^2.0.3",
    ...
    "socket.io": "^2.3.0",
    ...
  },
  ...
  }
}

Last but not least, for proving that the packages or modules is exist after the installation, just execute the following command to check it :

C:\node\app>npm list --depth=0
client@0.1.0 C:\node\chat-app\client
...
+-- cors@2.8.5
+-- express@4.17.1
+-- nodemon@2.0.3
...
+-- socket.io@2.3.0
...
C:\node\app>

As the command execution above, using ‘npm list –depth=0’ to list all the installed package or module in the application with only the first tree level without printing any further packages or modules dependencies, the package installed before are listed and printed in the above output command.

One thought on “How to Install a Package using npm tool

Leave a Reply