npm – the Node Package Manager

This is a short introduction to npm and its commands.

npm – the Node Package Manager

Npm – the Node Package Manager – is used to install Node programs. And also to specify and link dependencies between programs used in a package (or required to develop a new program).

To install npm you install Node (the two programs come together). Visit the Node website and download the installer for your platform.

npm commands

The command npm install package_name will install the latest version of a package.

You can install Node packages globally or locally.  You install packages locally that are only needed for your project. Installing locally (inside your project folder) is the default option. To install a package globally use the parameter -g (e.g. npm install -g yo).

To uninstall a package from your machine use npm uninstall package_name.

And the command to update a package already installed is npm update package_name.

If you want to know which other command you can use with npm the command npm help will give you a list of those commands. More information about a particular npm command can be found with npm help command. The last command will open a web page with information about a particular npm command in your browser.

To see which node packages are already installed for your project open the command line or terminal, go to your project folder and use npm ls.

You can find out which Node packages are publicly available with the command npm search. But there are so many packages that seeing them scroll past in the terminal is not very helpful. You’ll have better results if you use this command with a search term (e.g. npm search twitter-bootstrap).

If you want  more details about a package use npm info package_name. This will show you the contents of the “package.json” file. This file sits in the root of your project. It tells npm how your package is structured and what to do to install it.

dependencies

The “package.json” file contains the name and version of the package. But it also lists the dependencies and development dependencies (devDependencies). It’s pretty rare when a developer creates a tool that doesn’t depend on packages that were created before. Dependencies deal with packages that are required to run the application. Development dependencies are only needed during the development of an application.

Below you can see what the “package.json” looks like for the tutorial about how to setup a productive Bootstrap workflow:

contents of package.json file listing name, version and depencencies

To actually view the contents of a “package.json” file sitting in the root of a package you installed you can use the Online JSON editor.

Packages already come with their own “package.json” file. But if you need to create a new “package.json” file use the command npm init (without any arguments). This will start an interactive process to create a “package.json” file. This will become useful when your own project becomes a package that you want to publish. You can also edit a (new) “package.json” file with the Online JSON editor already mentioned above. To add a development dependency to a “package.json” file you can use the command npm install package_name --save-dev. This will install an existing package you want to use during the development of your app and at the same time insert it with the other devDependencies in the “package.json” file for your app.

Add your comment