Node.js
Node.js is a JavaScript runtime environment combined with useful libraries. It uses Google's V8 engine to execute code outside of the browser. Due to its event-driven, non-blocking I/O model, it is suitable for real-time web applications.
Installation
Alternate installations
It is not uncommon to need or desire to work in different versions of nodejs. A preferred method among node users is to use NVM (Node Version Manager). nvmAUR allows for cheap and easy alternative installs.
You can set it up by adding this to your shell's startup file:
# Set up Node Version Manager export NVM_DIR="$HOME/.nvm" # You can change this if you want. export NVM_SOURCE="/usr/share/nvm" # The AUR package installs it to here. [ -s "$NVM_SOURCE/nvm.sh" ] && . "$NVM_SOURCE/nvm.sh" # Load NVM
Usage is well documented on the project's GitHub but is as simple as:
$ nvm install 8.0 Downloading and installing node v8.0.0... [..] $ nvm use 8.0 Now using node v8.0.0 (npm v5.0.0)
Node Packaged Modules
npm is the official package manager for node.js. It can be installed with the npm package.
Managing packages with npm
Installing packages
Any package can be installed using:
$ npm install packageName
This command installs the package in the current directory under node_modules
and executables under node_modules/.bin
.
For a system-wide installation global switch -g
can be used:
# npm -g install packageName
By default this command installs the package under /usr/lib/node_modules/npm
and requires root privileges to do so.
Allow user-wide installations
To allow global package installations for the current user you want to specify the --prefix
parameter, e.g.:
$ npm -g install packageName --prefix ~/.node_modules
This is however not recommended since you need to remember the location and give it as the parameter each time you do an operation.
Using environment variables may be a more preferred solution:
~/.profile
PATH="$HOME/.node_modules/bin:$PATH" export npm_config_prefix=~/.node_modules
Re-login or source to update changes.
Updating packages
Updating packages is as simple as
$ npm update packageName
For the case of globally installed packages (-g
)
# npm update -g packageName
prefix
is set to a user-writable directoryUpdating all packages
However, sometimes you may just wish to update all packages, either locally or globally. Leaving off the packageName npm
will attempt to update all packages
$ npm update
or add the -g
flag to update globally installed packages
# npm update -g
Removing packages
To remove a package installed with the -g
switch simply use:
# npm -g uninstall packageName
to remove a local package drop the switch and run:
$ npm uninstall packageName
Listing packages
To show a tree view of the installed globally packages use:
$ npm -g list
This tree is often quite deep. To only display the top level packages use:
$ npm list --depth=0
To display obsolete packages that may need to be updated:
$ npm outdated
Managing packages with pacman
Some node.js packages can be found in Arch User Repository with the name nodejs-packageName
.
Troubleshooting
node-gyp python errors
Some node modules use the node-gyp
utility which does not support Python 3, which in most cases will be the default system-wide Python executable. To resolve such errors, make sure you have python2 installed, then set the default npm Python like so:
$ npm config set python /usr/bin/python2
In case of errors like gyp WARN EACCES user "root" does not have permission to access the ... dir --unsafe-perm option might help:
$ sudo npm install --unsafe-perm -g node-inspector
Additional resources
For further information on Node.js and the use of its official package manager NPM you may wish to consult the following external resources
- Node.js documentation
- NPM documentation
- IRC channel #node.js on irc.freenode.net