Node-js

From regional-training
  • Bulleted list item

Installation

How to install node.js. node.js is required by versus servers I have deveoped.

globally

Instructions can be found[1] for node-js installation via a package manager. The following sections outline that process on the designated Operating Systems.

Redhat

  • Configure your system to obtain software, including the latest dynamic languages, from the RHSCL software repository (or a local repos if you are in a restricted access enterprise system).

Instructions are provided for both the command line (CLI) and graphical user interface (GUI). The instructions in this section are only for systems using RHSM. The remainder of this tutorial applies to systems running either RHSM or RHN Classic.

Using the Red Hat Subscription Manager GUI

  • Start Red Hat Subscription Manager using the System Tools group of the Applications menu. Alternatively, you can start it from the command prompt by typing
subscription-manager-gui.
  • Select Repositories from the System menu of the subscription manager.

In the list of repositories, check the Enabled column for rhel-server-rhscl-7-rpms and rhel-7-server-optional-rpms (or rhel-server-rhscl-6-rpms and rhel-6-server-optional-rpms). If you are using a workstation version of Red Hat Enterprise Linux, the repositories will be named rhel-workstation-rhscl-7-rpms and rhel-7-workstation-optional-rpms (or rhel-desktop-rhscl-6-rpms and rhel-6-desktop-optional-rpms). Note: After clicking, it might take several seconds for the check mark to appear in the enabled column.

If you don’t see any RHSCL repositories in the list, your subscription might not include it. See Troubleshooting and FAQ for more information.

  • verify your installation via
node -v
npm -v
npx -v

Using subscription-manager from the command line

You can add or remove software repositories from the command line using the subscription-manager tool as the root user. Use the --list option to view the available software repositories and verify that you have access to RHSCL:

su -
subscription-manager repos --list | egrep rhscl

If you don’t see any RHSCL repositories in the list, your subscription might not include it. See Troubleshooting and FAQ for more information.

If you are using a workstation edition of Red Hat Enterprise Linux, change -server- to -workstation- in the following commands:

subscription-manager repos --enable rhel-server-rhscl-7-rpms
subscription-manager repos --enable rhel-7-server-optional-rpms

or for RHEL 6:

subscription-manager repos --enable rhel-server-rhscl-6-rpms
subscription-manager repos --enable rhel-6-server-optional-rpms

Setup your Node.js development environment

su -
yum install rh-nodejs10

Enable

scl enable rh-nodejs10 bash
node --version

Debian

Debian package Installation

  • You may install the Debian packaged node.js - which will not be the current release. It is advisable to look at the PPA installation for the JavaScript Requirements instead (skip this apt step).
 sudo apt install nodejs npm
  • To verify that the install was successful, run the node command with the -v flag to get the version:
   node -v
  • or you can run this:
sudo apt-get install curl software-properties-common
curl -sL https://deb.nodesource.com/setup_14.x | sudo bash -

Latest Debian

You should follow these instruction for the latest Debian installation [1]

via Debian PPA installation

To work with a more recent version of Node.js, you can install from a PPA (personal package archive) maintained by NodeSource. This is an alternate repository that still works with `apt, and will have more up-to-date versions of Node.js than the official Debian repositories. NodeSource has PPAs available for Node versions from 0.10 through to 12.


If you want to check what will happen:

  • download the following script from your home directory, using curl to retrieve the installation script for your preferred Node.js version, making sure to replace 12.x with your preferred version string (if different):
 cd ~ 
 curl -sL https://deb.nodesource.com/setup_14.x 
  • examine the script to see what it does.

Alternatively:

  • install with faith by downloading with immediate execution:
# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_14.x | bash -

The script will add a PPA to your apt manager configuration, and your local package cache will be updated automatically. Now you can install the nodejs package using apt via:

sudo apt install nodejs

The npm and npx applications are package in the nodejs package.

  • Verify the installation by running node with the -v version option:
node -v
npm -v
npx -v

locally

This is actually the preferred installation method since it gurantees isolation from the Operating System installation.

via nvm (preferred)

An alternative to installing node.js through apt is to use a tool called nvm [2] , which stands for “Node Version Manager”. Rather than working at the operating system level, nvm works at the level of an independent directory within your user’s home directory. This means that you can install multiple self-contained versions of Node.js without affecting the entire system.

Controlling your environment with nvm allows you to access the newest versions of Node.js while also retaining and managing previous releases, and not affecting the system deployment. It is a different utility from apt, however, and the versions of node.js that you manage with it are kept distinct from those you manage with apt.

  • To download the nvm installation script from the project’s GitHub distribution page, you can use curl. Note that the version number may differ from what is highlighted here:
curl -sL https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh -o install_nvm.sh

Inspect the installation script with nano:

nano install_nvm.sh

If the script looks OK, exit your text editor and run the script with bash:

  • install via script:
bash install_nvm.sh

You don’t need sudo here because nvm is not installed into any privileged system directories. It will instead install the software into a subdirectory of your home directory at ~/.nvm. It will also add some configuration to your ~/.profile file to enable the new software.

To gain access to the nvm functionality, you’ll need to either log out and log back in again or source the ~/.profile file so that your current session knows about the changes:

  • source your .profile
source ~/.profile

With nvm installed, you can install isolated node.js versions.

  • list remote for information about the versions of node.js that are available, type:
nvm ls-remote
  • install specific version via nvm
nvm install 10.16.2

Usually, nvm will switch to use the most recently installed version. You can tell nvm to use the version you just downloaded by typing:

  • run with a specific version in nvm
nvm use 10.16.2

As always, you can verify the Node.js version currently being used by typing:

  • verify
node -v

Output:

v10.16.2

practices

empty project

  • create an empty project with
npm init

package.json

  • use package.json
    • development dependencies kept separate from production dependencies
    • install node dependencies with
npm install --save-dev
  • install production dependencies with
npm install --save
dependencies will be stored in local node_modules

pm2

There is also the portable pm2 manager [3].

npm repository

The npm registry repository is maintained at https://registry.npmjs.org. A local instance of this will be required on the development (high) side.

The real registry reports that it is a couch_bt_engine - which is a HTTP documents database. See https://docs.couchdb.org/en/stable/ and [4] https://couchdb.apache.org/[5]

Couchdb is a NOSQL database that stores json objects et al.[6]



Rather than running your own instance of the above registry, someone has already written local-npm, a proxy-cache npm server that can be used to cache artefacts to optimise traffic (this will work in the interim, others use a nexis server as a node repository, and no doubt othersuse JFrog artifactory ). local-npm will require connectivity to the www to access https://registry.npmjs.org to resolve artefacts that are not cached. It also checks that versions are up to date, so it is a good thing to install on a server e.g. (padme.server - which has spare storage, maybe after the debain-repository).

mkdir /local-repos
cd /local-repos
npm install -g local-npm
  • start the server
$ local-npm
  • set npm to point to the local server:
$ npm set registry http://127.0.0.1:5080
  • To switch back, you can do:
$ npm set registry https://registry.npmjs.org

More switching using npmrc

  • install
npm install -g npmrc
npmrc -c local
  • switch
npm set registry http://127.0.0.1:5080
  • search using the default settings (proper remote repository)
npmrc default
npm list etc
  • and to switch back to local cache
npmrc local

Do your stuff

References

categories