Starting with NPM

What is npm? What can I do with it?
Npm is a package manager for Node.js and Javascript. Simple as that. You can manage and keep track of your project dependancies, or modules, and on some extent it can help you automating some tasks and handling all types of optimization, minification and transpiling.
Npm keeps tracks of your installed packages in a package.json file, enabling you to decide which packages are useful to production and to development. This will allow you and the other people to be perfectly aligned on the libraries and modules installed and used.

What is a package?
A package is just a directory with one or more files in it, that also has a file called “package.json” with some metadata about this package. A typical application, such as a website, will depend on dozens or hundreds of packages. These packages are often small. The general idea is that you create a small building block which solves one problem and solves it well. This makes it possible for you to compose larger, custom solutions out of these small, shared building blocks. When you install a package you could be notified of all improvements and changes made by package authors to the package itself.

How to install?
It is installed together with Node.js, so you should go to nodejs.org and install the lastest version of nodejs and NPM.
Npm modules can be installed globally (with -g flag) or locally. A global installed module will be available from every local package. A local package instead is initialized inside your project with npm init and adding packages with npm install (–save/–save-dev).

How to add and remove packages? 
First of all, there must be a package.json inside your project folder, so if it is not present, you should init npm using

This will execute a semi-automatic procedure that will ask you some details about your package, and then create your package.json file.
At this point your package file is obviously empty, with no module installed. You can check this by opening the file in every text editor you use.

At this point, let’s try and add some module. We will install Express, a routing library for Node. Open your terminal and type:

You may have to wait a bit for npm to finish downloading the module. As long as it is done, you will find out that a special folder node_modules has been added to your project, and guess what? It has the package you asked npm to install inside. Comfortable right?

You should always ask yourself if a specific module is needed for production or development before installing. As you could have noticed, a dependencies property has been automatically added to your package.json, containing the package you installed now. That is because we used the npm parameter –save that tells it to store the installed package into the dependencies property of the package. Dependencies property is used to store production dependencies. Let’s try and add a development dependency now. We will add webpack, a really useful automation package. Inside your terminal, type:

We used the –save-dev parameter now, and if you check the package.json file, the package has been added to another property, devDependencies, that indicates the dependencies used for development. 

To remove an installed package, you simply change npm install with npm uninstall, like this:

Considerations

NPM could be your best friend at automating processes during development. It is usually used to install and run Grunt, Gulp or Webpack to minimize, uglify and much more. It can be used to automate tests on your code, to lint your scripts and even to transpile every Javascript dialect to standard Javascript. You may create your packages, and submit them to the npm registry (https://docs.npmjs.com/misc/registry). I suggest you take a look to the npm registry and see for yourself which kind of packages you can find there. Apart from automating, another big use case for npm module is handling packages used by a Node.js application.

Enjoy using npm!

Leave a Reply