Node JS NPM

Node JS NPM
Node JS NPM

Introduction to npm

In the last article, you learned how to get started with integrating NodeJS in your apps. In this guide, you will learn how to install and use packages from Node JS NPM.

What is NPM and why use it?

NPM is a package manager for the NodeJS library. Via NPM, developers can use a plethora of modules which allows the programmer to extend their program’s functionality without writing complicated amounts of code.
Furthermore, it is also a host for a multitude of third party packages to enhance your project. Some famous examples include:

  • express : lets you build a web server and perform server side operations with ease.
  • morgan: logs HTTP requests, which gives the developer concise insight on their app’s performance and reliability.

Usage of NPM consists of two parts:

  • a terminal interface: Will help you publish and download packages.
  • online repository: hosts the JavaScript packages

Here is a visual explanation:

Now that we have covered a brief introduction to NPM, let’s get started!

NPM: The fundamentals

Project creation with npm

As the first step, you have to first tell NPM to initialize your repository. To do so, use the following terminal command in your project folder:

mkdir beginners #create a folder
cd beginners #go into this folder
npm init -y #finally, initialize repository

`This will be the output in the console:

As a result, this will create a package.json file in your application folder:

Installing modules

If you want to install a package locally for your app, run the following terminal command:

npm i <package_name>
# for example, to install express
npm i express

Alternatively, to install a package globally, use the -g argument:

npm i -g <package_name>
# to install expo-cli:
npm i -g expo-cli

When to install packages globally? Use global packages for modules that are actually command line programs, for example expo-cli. Otherwise, install the package without the -g argument.
This will create a node_modules folder on your computer:

Additional NPM Commands

To see all available options, use the help command:

npm help

To get the current installed version, pass in the -v argument:

npm -v
# or
npm --version

To install a specific version of a package, for example, express:

npm install express@4.2.0

However if you want to install latest version of a package, (this is useful when a package requires an update):

npm install express@latest

You can even install more than one package within one command like so:

npm i react react-dom babel babel-core

To update npm to its latest version, type

npm i -g npm@latest

To remove a module: (for example, **express)

npm rm express

The Package.json file

Notice that if we use NPM in our app, the project automatically creates a package.json file. What is a package.json file?

Simply put, a package.json file is a manifest of your project that includes the installed packages and and dependencies. Furthermore, it also covers info about its unique source control, and specific data like the project’s name, description, version, and author.
Here are the core parts of this file:

{
  "name": "myproject", // The name of your project
  "version": "0.0.1", // The version of your project
  "description": "Creates a simple project.", // The description of your project
  "main": "index.js"
  "license": "MIT" // The license of your project
  "dependencies": { //installed modules
    "fill-keys": "^1.0.2",
    "module-not-found-error": "^1.0.0",
    "resolve": "~1.1.7"
  }
}

The package.json file always follows the JSON format, which allows it to be easily read as metadata and parsed by machines.

Conclusion

In this article, you learned how to use and install packages from the Node JS NPM repository. This is one of the most useful skills for any JavaScript beginner.

Thank you so much for reading! Happy coding!

In the next article, you will learn the fundamentals of Express.

Scroll to Top