AppDividend
Latest Code Tutorials

Introduction To Node Package Manage | What is NPM

0

Introduction To Node Package Manager(NPM). Today NPM is the biggest repository for any programming language, and it has almost every package that you need in a web or mobile development project. The npm means the node package manager

The topics we cover are the following.

NPM Topics

  1. Overview of NPM
  2. Attributes of package.json
  3. Updating NPM packages
  4. NPM Versioning
  5. Semantic versioning
  6. Global and local installation of NPM packages.
  7. Uninstalling module.
  8. Search a module
  9. Running an NPM task

#Introduction To Node Package Manager(NPM)

If you have previous work with any front-end development stuff, then this package manager is familiar to you. 

In January 2017 over 350,000 packages were reported being listed in the npm registry, making it the most significant single language code repository on the planet. At the starting point Node Package Manager is only for Node.js development and nothing else, but as Javascript community grows, it becomes the backbone of almost all the latest JavaScript Frameworks and Web development. 

The npm registry hosts the world’s most extensive collection of free, reusable code.

NPM is written entirely in JavaScript(JS) and was developed by Isaac Z. Schlueter with inspiration from the shortcomings of other similar projects such as PEAR (PHP) and CPAN (Perl).

You can go to its original website: https://www.npmjs.com/. The Node Package Manager or NPM manages downloads of dependencies of your project.

If you have installed Node.js in your machine then by default NPM has already been installed. The Node.js comes with NPM.

You can check the version of your NPM using the following command.

An Introduction To Node Package Manager(NPM)

All of your dependencies are written in one file inside your project root called package.json.

You can create the package.json file using the following command.

npm init

If you have already the package.json file, but dependencies are not installed previously then you can install all the dependencies defined on your package.json file using the following command.

npm install

If you want to install development dependency, then you can hit the following command with the  –dev flag.

npm install <package-name> --save-dev

# or

npm install -D <package-name>

The difference between dependency and devDependencies is usually development tools, like a testing library, while dependencies are bundled with the app in production.

If you do not want to write install, then you can use i, and it will do the work for you.

npm i <package-name>

If you want to save the dependency inside the package.json filethen you need to add the –save flag.

npm i <package-name> --save

NPM will generate one folder called node_modules when you install any library from the Node Package Manager Repository.

#Usage

NPM can manage packages that are local dependencies of a particular project, as well as globally-installed JavaScript tools. We can use it as a dependency manager for our local project.

#Attributes of Package.json

  • name − the name of the package
  • version − version of the package
  • description − description of the package
  • homepage − homepage of the package
  • author − author of the package
  • contributors − the name of the contributors to the package
  • dependencies − list of dependencies. NPM automatically installs all the dependencies mentioned here in the node_module folder of the package.
  • repository − repository type and URL of the package
  • main − entry point of the package
  • keywords − keywords

#Updating NPM Packages.

You can update NPM packages using the following command.

npm update

You can also update the specific package using the following command.

npm update <package-name>

If you are running an old version of NPM, then you can update it to the latest release by the following command from the root.

sudo npm install npm -g

#NPM Versioning.

In addition to plain downloads, the node package manager also manages the versioning so that you can specify any specific version of a package.

If you specify the exact version of the NPM libraries, then it also helps to keep everyone on the same version of a library, so that the whole team runs the same version and no conflicts occur until the package.json file is updated.

If you are using the Git version control system, then you need to upload the package.json file and not a node_modules folder.

So when another developer downloads the project, it has already the package.json file, and he only needs to hit the npm install command to up and running with the project.

If we do not specify any version, then it will install the latest version of the particular package.

#Semantic Versioning

  • To install a package of a specific version, mention the full and exact version in the package.json file.
  • To install the latest version of the package, mention “*” in front of the dependency or “latest”. This will find the latest stable version of the module and install it.
  • To install any version (stable one) above a given version, mention it like in the example below:
    “express”:”^4.1.1″. in the package.json file. The caret symbol (^) is used to tell the npm to find a version greater than 4.1.1 and install it.

#Global vs. Local Installation.

By default, NPM installs any dependency in the local mode. Here local mode refers to the package installation in the node_modules directory inside our working project.

Locally deployed packages are accessible via the require() method.

For example, when we installed an express module, it created a node_modules directory in the current directory where it installed the express module.

$ npm install express

Now you can use this module in your js file as following −

var express = require('express');

Globally installed packages are stored in the system directory. Such dependencies can be used in the CLI (Command Line Interface) function of any node.js but cannot be imported using require() in Node application directly. Now let’s try installing the express module using global installation.

npm install express -g

It will produce a similar result, but the module will be installed globally.

#Uninstalling a Module

Use the following command to uninstall a Node.js module.

npm uninstall <package-name>

#Search a Module

Search a package name using NPM.

npm search express

#Running Tasks

The package.json file supports a format for specifying command-line tasks that can be run by using the following command.

npm <task-name>

For example:

{
  "scripts": {
    "start-dev": "node lib/server-development",
    "start": "node lib/server-production"
  },
}

It’s very common to use this feature to run Webpack:

{
  "scripts": {
    "watch": "webpack --watch --progress --colors --config webpack.conf.js",
    "dev": "webpack --progress --colors --config webpack.conf.js",
    "prod": "NODE_ENV=production webpack -p --config webpack.conf.js",
  },
}

So instead of typing those extended commands, which are easy to forget or mistype, you can run

$ npm watch
$ npm dev
$ npm prod

Finally, Introduction To Node Package Manager(NPM) Example is over. Thanks for taking it.

Recommended Posts

What is process.env in Node.js

How to build Nodejs web server

Node Streams Example

AWS Lambda And Node.js Example

ES6 Modules in Node Example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.