AppDividend
Latest Code Tutorials

An Introduction To Node Package Manager(NPM)

293

Get real time updates directly on you device, subscribe now.

An 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. npm means the node package manager. 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.

#An Introduction To Node Package Manager(NPM)

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.1

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 are 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 as a dependency manager for our local project.

#Attributes of Package.json

  • name − 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 − 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 root.

sudo npm install npm -g

#NPM Versioning.

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

Related Posts
1 of 17

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 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 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

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, An Introduction To Node Package Manager(NPM) is over. Thanks for taking.

Leave A Reply

Your email address will not be published.

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