Installing NPM Command-Line Applications at the User Level on Ubuntu
Recently, I’ve started to see more command-line applications written in JavaScript and meant to run on Node.js. Here are some notes on my preferred way to install them.
Purpose
This assumes you are running the Ubuntu 14.04 operating system and that you want to:
- use the Ubuntu-provided versions of Node.js and NPM,
- use the NPM-provided version of some command-line application which runs on top of Node.js, and
- install the command-line application for one user, without using superuser privileges to muck with your system and install it for all users.
If that’s not what you want, try this.
Steps
All the code
below is meant to be typed into the command-line.
Get the Ubuntu-provided Node.js and NPM
sudo apt-get install nodejs npm
Set Up ~/bin
There’s a typical way to install things at the user level, and it’s explained on the Ubuntu help wiki.
If you just want to do it now and understand it later:
cd
mkdir bin
echo >> .bashrc 'export PATH="$HOME/bin:$PATH"'
source .bashrc
Allow Node.js to Run as node
The Ubuntu-provided version of Node.js is installed under the name nodejs
, but the NPM provided applications ask for node
. Bridge that gap like so:
ln -s `type -p nodejs` ~/bin/node
Install the Actual Application You Want to Use
I’ll use nodeunit
as an example of an application to install and use.
By default, NPM installs things into the current directory. That’s mainly for people installing libraries into the directory of the application they’re writing, but we can use it to install applications into our ~/bin
.
cd ~/bin
npm install nodeunit
ln -s node_modules/.bin/nodeunit .
That should leave you with a ~/bin/nodeunit
that refers to something inside ~/bin/node_modules
.
(Instead of using ln
on each application, you can also just add ~/bin/node_modules/.bin
to your $PATH
, the same way you added ~/bin
. I don’t like to do that because it adds all commands from all packages and their dependencies.)
Result
At this point you should be able to run the application like any other.
Example Command
nodeunit --help
Output:
Usage: nodeunit [options] testmodule1.js testfolder [...]
Options:
--config FILE the path to a JSON file with options
--reporter FILE optional path to a reporter file to customize the output
--list-reporters list available build-in reporters
-t testName, specify a test to run
-f fullTestName, specify a specific test to run. fullTestName is built so: "outerGroup - .. - innerGroup - testName"
-h, --help display this help and exit
-v, --version output version information and exit