a blog for those who code

Wednesday 8 March 2017

Getting Started with Node Package Manager

In this post we will be discussing about getting started with Node package manager a.k.a npm. Node.js is an open source, cross-platform run-time environment for developing server-side web applications. The main idea of Node.js is to use non-blocking, event-driven I/O to remain lightweight and efficient to build real-time applications which will run across distributed devices.

Node Package Manager (npm) is the package manager for Node.js. It is a command line client which interacts with a remote registry that allows developers to install and publish packages. 

Before getting started with npm, we first have to install Node.js on our system. After Node.js installation is completed, we can check that npm is installed or not using the command  npm 

Npm is installed along with node, but it keeps updated. So to get the latest version of npm installed in your system you can run command

 npm install npm@latest -g 

Installing Packages though npm

There are mainly two ways you can install a package through npm : locally or globally. If you want a package only for a certain application or module than you can install it locally (npm's default behaviour). On the other hand if you want to use a package as a command line (for example npm) then you can install it globally.

Install NPM Package Locally

You can install the npm package in local mode which will be installed in a node_modules folder in your parent working directory. The location is owned by the current user. Its a default behaviour of npm. To install pincode locally we will run the command

 npm install pincode 

Install NPM Package Globally

You can install npm package globally which will be installed In Windows (\AppData\Roaming\npm\node_modules) and in {prefix}/lib/node_modules in linux and MAC which is owned by root. For installing in Linux and MAC you need to use sudo to install package globally but for windows you can directly run the below command to install pincode package globally.

 npm install pincode -g 

Note : You can also list all the Global Packages installed using the command  npm list -g --depth=0  as shown below :

Installing Specific Version of a package

You can also install a specific version of a package for that you need to append a version number after the package name as shown below.

 npm install grunt@0.4.5 

The above command will install 0.4.5 release version of grunt.

Uninstalling Packages through NPM

You can uninstall a package through npm using the command   npm uninstall . For local package you have to remove the package using the command  npm uninstall pincode inside the folder where you have installed it whereas for global packages you have to uninstall using the command  npm uninstall -g pincode .

How to solve npm Error : ENOENT, stat in Nodejs while installing the package

To solve "Error: ENOENT, stat" error you need to manually create a folder named 'npm' inside 'C:\Users\Me\AppData\Roaming\'. After doing this your install would be successful. Just for an information AppData is a hidden folder. You need to check "Show hidden files,folders, and drives".

What is NodeJS - npm WARN package.json : No repository field ?

This warning means that the package does not have the repository field in the package.json. This field is used just for informational purposes. Suppose you are an author of a package and you are getting this error, you can write the below code in your package.json to remove this warning.

"repository": {
    "type" : "git",
    "url" : "Your_Package_URL"

Please Like and Share CodingDefined.com blog, if you find it interesting and helpful.

No comments:

Post a Comment