nodejs - done reading

npm abbreviates for Node Package Manager.

If we have to use sudo to get around permission issue, consider doing:

npm config set prefix ~/.npm
echo 'export PATH="$PATH:~/.npm/bin"' >> ~/.bashrc

To address permission issue on Windows, consider doing:

mkdir /c/Users/$USER/AppData/Roaming/npm
echo 'export PATH="$PATH:/c/Program Files/nodejs"' >> ~/.bashrc

The above code assumes that we are using Git Bash.  On 32-bit Windows, use:

echo 'export PATH="$PATH:/c/Program Files (x86)/nodejs"' >> ~/.bashrc 

npm install --global packageName
npm rm --global packageName

cd projectFolderName
npm init // creates the package.json file and the node_modules folder

npm install --save-dev packageName 

Most users will not have permission to write to the global directories unless 
npm commands are prefixed with sudo. There are a number of options to fix npm 
permissions and tools such as nvm can help but I often change the default 
directory, e.g. on Ubuntu/Debian-based platforms:

cd ~
mkdir .node_modules_global
npm config set prefix=$HOME/.node_modules_global
npm install npm -g

Then add the following line to the end of ~/.bashrc:

export PATH="$HOME/.node_modules_global/bin:$PATH"

and update with:

source ~/.bashrc

npm will not install a package as a development dependency for a project when 
the NODE_ENV environment variable is set to "production".

The difference between 'npm --save' and 'npm --save-dev' is that 'npm --save' 
adds the package under the 'dependencies' section in package.json file, and 
'npm --save-dev' adds the package under the 'devDependencies' section in the 
package.json file.  Development dependencies are different from dependencies for 
the project itself.  Development dependencies are things that are not used by 
the product, but are only used to aid the developers to produce the product.

So, what happens when we run 'npm install'?  Will it install both dependencies 
that are listed under the 'dependencies' section and those that are listed 
under the 'devDependencies' section?  I do not know the answer this question.  

If we are behind a firewall that blocks npm, and you have a proxy that allow 
access to npm servers, consider using:

npm config set proxy <proxy server url>

npm install -g http-server
http-server app/__build

What happens if we run 'npm install' without specifying the -g flag, or the 
'--save-dev' flag?

What is npm?

It is a package manager for the NodeJS platform.

What are the main features?

  • It installs application dependencies locally, not globally.
  • It handles multiple versions of the same module at the same time.
  • You can specify tarballs or git repositories as dependencies.
  • It's really easy to publish your own module to the npm registry.
  • It's useful for creating CLI utilities that others can install (with npm) and use right away.
  • npm support multiple version of Node


How can we install npm?

sudo apt-get update 
sudo apt-get install build-essential libssl-dev

curl | bash

How can we use npm to support multiple versions of Node?

See the main nodejs page.

How can we use npm to install a module, package, library?

npm install express

What is the purpose of 'npm init'?

npm init
npm install can steal --save

The 'npm init' command initialize "scaffold" (sort of) a node application. It creates the package.json file, which is used track dependencies, and other information.

What is the purpose of the package.json file?

It is used to track dependent modules.

What is the purpose of the --save flag?

npm install request --save

The above command install the 'request' module and update the package.json file.

What happens when we install a npm module globally?

npm can install packages in local or global mode. 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.

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.

What is the purpose of the 'npm config' command?

npm config list
; cli configs
registry = ""
user-agent = "npm/2.6.0 node/v0.10.36 linux x64"
; node bin location = /usr/local/bin/node
; cwd = /home/sitepoint
; HOME = /home/sitepoint
; 'npm config ls -l' to show all defaults.

It tells us information about our install.

npm config get prefix

The 'npm config' command also allow us to customize some of the settings.

How can we configure npm so that we can install npm package globally such that they are installed in our own home directory?

To install global packages in our home directory:

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
cat .npmrc

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

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 or .bash_profile 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
npm -v

How can we list global packages?

npm list --global
npm list -g --depth=0

The above command displays a list of npm packages that were installed globally.

What happens when we install an npm package locally?

Installing packages in local mode is done without the —global flag. The package will be installed in your parent working directory in a node_modules folder.

How can we list local npm packages?

npm list

How can we re-install a local npm package?

npm is a package manager so it must be able to delete a package. Let’s assume that the current underscore package is causing us compatibility problems. We can delete the package and install an older version, like so:

npm uninstall underscore
unbuild underscore@1.8.2
npm list
└── (empty)

npm install underscore@1.7.0
underscore@1.7.0 node_modules/underscore
npm list
└── underscore@1.7.0

How can we update an npm package to the latest version?

The latest version of underscore fixed the bug we had earlier and we want to update our package to that version:

npm update underscore

How can we clean the npm cache?

When npm installs a package it keeps a copy, so the next time you want to install that package, it doesn’t need to hit the network. The copies are cached in the .npm directory in your home path. This directory will get cluttered with old packages over time, so it’s useful to clean it up occasionally.

npm cache clean

What is the purpose of the 'scripts' entry in the package.json file?

A sample package.json file:

  "name": "buildtool",
  "version": "1.0.0",
  "description": "npm as a build tool",
  "dependencies": {},
  "devDependencies": {},
  "scripts": {
    "info": "echo 'npm as a build tool'"
  "author": "SitePoint",
  "license": "ISC"

As you can see, we have a scripts object with a property called info. The value of info is going to be executed in the shell as a command. We can see a list of the scripts properties (also known as commands) and values defined in a project by running the command:

npm run

If you run the previous command in our project folder, you should see the following result:

Scripts available in buildtool via `npm run-script`:
    echo 'npm as a build tool'

In case you want to run a specific property, you can run the command:

npm run <property>

So, to run the info command we defined in the package.json file, we have to write:

npm run info

If you only want the output of info, you can use the -s flag which silences output from npm:

npm run info -s
npm as a build tool

We only used a simple echo so far, but this is a very powerful feature. Everything on the command line is available to us and we can be very creative here.

Next, we have to add a property to our scripts object in package.json that will trigger jshint. To do that, we’ll create a lint property as follows:

"scripts": {
  "info": "echo 'npm as a build tool'",
  "lint": "echo '=> linting' && jshint assets/scripts/*.js",
  "test": "echo '=> testing' && mocha test/"

Now, we can run the lint command:

npm run lint -s

npm has a few convenient shortcuts, namely npm test, npm start and npm stop. These are are all aliases for their run equivalents, which means we just need to run npm test to kick mocha into action:

What are the pre and post hooks?

It wouldn’t be very efficient if we were to run our test suite and it bailed straight away because of a syntax error. Luckily npm gives us the pre and post hooks, so if you run npm run test it will first execute npm run pretest and npm run posttest when it finishes. In this case we want to run the lint script before the test script. The following pretest script makes this possible.

"scripts": {
  "info": "echo 'npm as a build tool'",
  "lint": "echo '=> linting' && jshint assets/scripts/*.js",
  "test": "echo '=> testing' && mocha test/",
  "pretest": "npm run lint -s"

Imagine we hadn’t have corrected the syntax errors in our script previously. In this case, the above pretest script will fail with a non-zero exit code and the test script won’t run. That is exactly the behavior we want.

What is the purpose of the watch package?

One of the things that Grunt, Gulp and their ilk are great at, is watching a set of files and re-running a specific task whenever one of those files is detected to have changed. This is particularly useful in cases such as this, as it would be a pain in the neck to re-run the minification scripts manually. The good news is that you can do that in npm, too, using a package such as watch, which is intended to make managing the watching of file and directory trees easier.

npm install watch --save-dev

Then in package.json, you need to specify the tasks to run when a change is detected. In this case JavaScript and CSS minification:

"scripts": {
  "watch": "watch 'npm run minify:js && npm run minify:css' assets/scripts/ assets/css/"

Start the script using:

npm run watch

Now, whenever any file in assets/scripts/ or assets/css/ changes, the minification scripts will be called automatically.

By now we have several scripts that we can chain together to make a build script which should do the following: linting, testing and minifying. It would, after all, be a pain to have to run these tasks individually time after time. To create this build script, alter the script object in package.json, thus:

"scripts": {
  "info": "echo 'npm as a build tool'",
  "lint": "echo '=> linting' && jshint assets/scripts/*.js",
  "test": "echo '=> testing' && mocha test/",
  "minify:js": "echo '=> minify:js' && uglifyjs assets/scripts/main.js -o dist/public/js/jquery.min.js",
  "minify:css": "echo '=> minify:css' && cleancss assets/css/main.css -o dist/public/css/main.min.css",
  "build": "echo '=> building' && npm run test -s && npm run minify:js -s && npm run minify:css -s",
  "pretest": "npm run lint -s"
npm run build -s
npm install http-server -save-dev
"scripts": {
  "server": "http-server dist/public/",
npm run server

What is nave?

Most nodejs frameworks provide some really handy binaries for the commandline (SailsJs, Kraken, Compound), none of which you’ll be able to use on linux if you do not install (Express) using the -g switch. But if you do use that switch on linux you then have to use sudo, which now puts you in the sticky situation of installing all your node framework binaries into one central location.

Using nave, means you can sandbox all your npm modules to a specific nodejs version and still get the node binaries without using sudo. Npm installing locally into ./node_modules is only half the step. Nave completes the rest of that momentum.

See the last comment at the bottom of

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License