Node Package Manager (npm)

Posted on 20th January 2017

Node Package Manager (npm) comes with standard node installation and it allow developers to easily install and update modules (packages) created by other developers. To check npm version,

C:\> npm -v

Node.js is lightweight and the core modules can be used for basic functionalities. You would want to use third-party packages or modules for most generic tasks. You can use npm to install packages and manage versions and dependencies for local projects as well as for globally installed tools.

The syntax to install a new module is:

C:\MyApp> npm install <Module Name>

By default npm installs the module in your local folder. ie, If your node application is in a folder called MyProject, the module will be installed in a folder named node_modules inside the MyProject folder. You can import the module in to your own code using require function. If you want to use the module as a command line tool, then you have to install it globally using the -g option. For example, following is the command to install a module called body-parser

C:\MyApp> npm install body-parser

Now you can use this module in your application as follows:

var bodyParser = require('body-parser');

To uninstall a module

C:\MyApp> npm uninstall <ModuleName>

Any node application or package will have a file called package.json with all the dependency and version information. You can create one for your own project so that you can manage all the dependencies. To build one for your project, run the command:

C:\MyApp> npm init

This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install  --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (nodejs)

There are other open source alternatives to npm like ied, pnpm, Yarn. It is also possible to have several versions of Node.js on the same machine. Multiple versions can be managed with tools such as nvm (Node Version Manager).

Post a comment


Nothing the first to share wisdom.