A Beginner’s Guide to npm — the Node Package Manager
Node.js makes it possible to write applications in JavaScript on the server. It's built on the V8 JavaScript runtime and written in C++ — so it's fast. Originally, it was intended as a server environment for applications, but developers started using it to create tools to aid them in local task automation. Since then, a whole new ecosystem of Node-based tools (such as Grunt, Gulp and Webpack) has evolved to transform the face of front-end development.
This popular article was updated on 08.06.2017 to reflect the current state of npm, as well as the changes introduced by the release of version 5.
To make use of these tools (or packages) in Node.js we need to be able to install and manage them in a useful way. This is where npm, the Node package manager, comes in. It installs the packages you want to use and provides a useful interface to work with them.
In this article I'm going to look at the basics of working with npm. I will show you how to install packages in local and global mode, as well as delete, update and install a certain version of a package. I'll also show you how to work with package.json
to manage a project's dependencies. If you're more of a video person, why not sign up for SitePoint Premium and watch our free screencast: What is npm and How Can I Use It?.
But before we can start using npm, we first have to install Node.js on our system. Let's do that now...
Installing Node.js
Head to the Node.js download page and grab the version you need. There are Windows and Mac installers available, as well as pre-compiled Linux binaries and source code. For Linux, you can also install Node via the package manager, as outlined here.
For this tutorial we are going to use v6.10.3 Stable. At the time of writing, this is the current Long Term Support (LTS) version of Node.
Tip: You might also consider installing Node using a version manager. This negates the permissions issue raised in the next section.
Let's see where node was installed and check the version.
$ which node
/usr/bin/node
$ node --version
v6.10.3
To verify that your installation was successful let's give Node's REPL a try.
$ node
> console.log('Node is running');
Node is running
> .help
.break Sometimes you get stuck, this gets you out
.clear Alias for .break
.exit Exit the repl
.help Show repl options
.load Load JS from a file into the REPL session
.save Save all evaluated commands in this REPL session to a file
> .exit
The Node.js installation worked, so we can now focus our attention on npm, which was included in the install.
$ which npm
/usr/bin/npm
$ npm --version
3.10.10
Node Packaged Modules
npm can install packages in local or global mode. In local mode it installs the package in a node_modules
folder in your parent working directory. This location is owned by the current user. Global packages are installed in {prefix}/lib/node_modules/
which is owned by root (where {prefix}
is usually /usr/
or /usr/local
). This means you would have to use sudo
to install packages globally, which could cause permission errors when resolving third-party dependencies, as well as being a security concern. Lets change that:
Changing the Location of Global Packages
Let's see what output npm config
gives us.
$ npm config list
; cli configs
user-agent = "npm/3.10.10 node/v6.10.3 linux x64"
; userconfig /home/sitepoint/.npmrc
prefix = "/home/sitepoint/.node_modules_global"
; node bin location = /usr/bin/nodejs
; cwd = /home/sitepoint
; HOME = /home/sitepoint
; "npm config ls -l" to show all defaults.
This gives us information about our install. For now it's important to get the current global location.
$ npm config get prefix
/usr
This is the prefix we want to change, so as to install global packages in our home directory. To do that create a new directory in your home folder.
$ cd ~ && mkdir .node_modules_global
$ npm config set prefix=$HOME/.node_modules_global
With this simple configuration change, we have altered the location to which global Node packages are installed. This also creates a .npmrc
file in our home directory.
$ npm config get prefix
/home/sitepoint/.node_modules_global
$ cat .npmrc
prefix=/home/sitepoint/.node_modules_global
We still have npm installed in a location owned by root. But because we changed our global package location we can take advantage of that. We need to install npm again, but this time in the new user-owned location. This will also install the latest version of npm.
$ npm install npm --global
└─┬ npm@5.0.2
├── abbrev@1.1.0
├── ansi-regex@2.1.1
....
├── wrappy@1.0.2
└── write-file-atomic@2.1.0
Finally, we need to add .node_modules_global/bin
to our $PATH
environment variable, so that we can run global packages from the command line. Do this by appending the following line to your .profile
, .bash_profile
or .bashrc
and restarting your terminal.
export PATH="$HOME/.node_modules_global/bin:$PATH"
Now our .node_modules_global/bin
will be found first and the correct version of npm will be used.
$ which npm
/home/sitepoint/.node_modules_global/bin/npm
$ npm --version
5.0.2
The post A Beginner’s Guide to npm — the Node Package Manager appeared first on SitePoint.
Comments
Post a Comment