Node.js

From ArchWiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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

Install the nodejs package. There are LTS releases too:

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
source /usr/share/nvm/init-nvm.sh

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)

If you decide to use nvmAUR, previously it was suggested to use nodejs-fake package from AUR. Which is now deleted. Suggested way is to use --assume-installed nodejs=<version>, as per the manual pacman(8) § TRANSACTION OPTIONS (APPLY TO -S, -R AND -U).

If you want to run `nvm use` automatically every time there is a .nvmrc file on the directory, add this in ~/.bashrc

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, set the npm_config_prefix environment variable. This is used by both npm and yarn.

~/.profile
PATH="$HOME/.local/bin:$PATH"
export npm_config_prefix="$HOME/.local"

Re-login or source to update changes.

You can also specify the --prefix parameter for npm install. However, this is not recommended, since you will need to add it every time you install a global package.

$ npm -g install packageName --prefix ~/.local

Another method to do the same is to edit prefix field in $HOME/.npmrc file to change the install location permanently.

Note: The last method is specific to npm only.

Updating packages

Updating packages is as simple as

$ npm update packageName

For the case of globally installed packages (-g)

# npm update -g packageName
Note: Remember that globally installed packages require administrator privileges unless prefix is set to a user-writable directory
Updating 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
Note: Remember that globally installed packages require administrator privileges

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 errors

In case of errors like gyp WARN EACCES user "root" does not have permission to access the ... dir, --unsafe-perm option might help:

# npm install --unsafe-perm -g node-inspector

Cannot find module ... errors

Since npm 5.x.x. package-lock.json file is generated along with the package.json file. Conflictions may arise when the two files refer to different package versions. A temporary method to solving this problem has been:

$ rm package-lock.json
$ npm install

However, fixes were made after npm 5.1.0 or above. For further information, see: missing dependencies

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