Thursday, December 7, 2017

Node.js breadcrumbs

Node.js is not properly installed until it's reliable in command line at the user level. Portions of node.js installed from Arch repositories are, of course, global to command line. The rest of the installation is mostly local to one project or another, as various modules become necessary.

global

For Arch, one can use pacman:
# pacman -S nodejs npm
These will work in any directory.

project

Suppose a user establishes a folder for their node.js project, and is working from that project directory. During their project, they recall that they need to install this or that module, say, express. The user roots up, runs # npm install express, then descends to user and continues working on their script, or some other operation which calls express. Eventually, to verify the express module's working, the user types "express" at the command line. Here's what would happen.
$ express
bash: express: command not found
Unlike pacman, npm is a node.js specific installer with its own directory structure. Node.js is roughly analogous to an independent LaTeX install: you can use $ tlmgr to update or control things, but not until the LaTeX directories are added to PATH. Likewise npm works from the command line, except that one must root-up when using it as an installer (# npm install [some package]). This is because the Arch version of npm places the modules into a root privilege directory /usr/lib/node_modules. This will hinder us throughout anything we do. We can place /usr/lib/node_modules/ in our PATH, which will allow execution, but we can never add or modify files in it. For this reason, I strongly believe that, like LaTeX, node.js should be installed in the HOME directory at user level, never requiring root privileges. If you install node.js with pacman, you'd have to pacman or yaourt every single module installed by node.js, or create PATH additions for each and every one that npm installs. No way, jose.
# pacman -Rs npm nodejs
$ rm -r .npm

nvm

For me, the easiest way to install a user-level node.js application was to use NVM volume manager to import node.js. NVM is upstream from node.js installations, so it can download and install any node.js version, allowing multiple releases to exist simultaneously on a system within the home director: users may select the version they wish to use directly from the user level.

The NVM script at the blog above downloads the latest git version of NVM, or just go directly to the install script here. I opened it with a word editor and reviewed it, then ran the script to download NVM.
$ sh ./install.sh
In addition to installing NVM at the user level, the install script appends to ~/.bashrc so the PATH is updated. I verified the changes, found them acceptable, and then sourced the .bashrc to activate the PATH change. Next, I verified nvm was detected and ran.
$ source .bashrc
$ nvm

npm

The next concept in user-space node.js is managing packages. Since node volumes can be placed using NVM, the packages for any version of node will be with a pacakage manager, NPM. it's good to use

No comments: