Node Package Manager (NPM)

Node Package Manager (NPM)

Node Package Manager consists of files built by other developers we can use in our projects to make development easier and faster. With NPM, we can make use of code written by others for a particular purpose in our application.

Imagine you are building a weather app that forecast the weather for different timezones. One of the things you would have to do is to get the accurate time for each timezone, this might entail you writing your block of code to achieve that. With NPM, we can simply make use of codes written by other programmers for getting timezones by installing them in our project.

NPM is massively popular in today's programming space with close to 2 Million packages being used by programmers today. Some popular packages being used today include lodash, react, tailwind css, express, etc.

To use NPM we make use of various Command Line Interface (CLI).

For instance, to set up NPM in our project, we use

npm init

This initializes a package.json file in our project after we have completed the prompted query like name**,** license, scripts, description, author, keywords, version, main file etc.

Package.json helps to keep track of all dependencies our projects rely on to function properly. These dependencies are simply packages installed from the Node Package Manager. A package.json file looks like this;

{
  "name": "MyAppName",
  "version": "0.0.0",
  "description": "A description of your app",
  "main": "app.js",
  "author": {
    "name": "cosmosDuck",
    "email": ""
  },
  "dependencies": {
    "express": "^4.13.3"
  }
}

We can install Node packages either locally or globally.

To install a node package locally means that we can only use that package in the current project directory. You do that with the code below;

npm install [package name]

Unlike the local setup, when a Node package is installed globally we can access it from whichever project directory we chose. To install globally use the code below;

npm install -g [package name]

Since these packages are mostly open-source and frequently maintained by their creator, most times, they will always be updated to be more effective and to get rid of bugs. So, each time they get updated we have to upgrade to the latest version currently in use. And you can do that with the code below;

npm update [package name]

To get a list of all the dependencies we have installed for a project we can simply use the code below;

npm list

While using the NPM, if you by any chance get lost using the command line, which we often do, you can simply use the code below to get a list of all recognized NPM command lines

npm help

To uninstall any Node package which you consider no longer relevant to your project, simply use the code below;

npm uninstall [package name]

In summary, npm is a very crucial aspect of programming as it gives us access to packages developed by other developers which makes our job a lot easier. You can learn more about it here.