Installing Node.js with NVM

Using NVM to juggle Node.js versions

The Problem

Installing NVM

Linux

export NVM_DIR="$HOME/.nvm" && (
git clone https://github.com/creationix/nvm.git "$NVM_DIR"
cd "$NVM_DIR"
git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1)`
) && \. "$NVM_DIR/nvm.sh"
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
# chose desired node version
VERSION
="8.12.0"
# install node
nvm install ${VERSION}
nvm use ${VERSION}

macOS

# chose desired node version
VERSION
="8.12.0"
# install nvm
brew install nvm
source $(brew --prefix nvm)/nvm.sh
echo 'source $(brew --prefix nvm)/nvm.sh' >> ~/.profile
# install node
nvm install ${VERSION}
nvm use ${VERSION}

Windows

# chose desired node version
$version = "8.12.0"
# install nvm w/ cmder
choco install cmder
choco install nvm
refreshenv
# install node
nvm install $version
nvm use $version

Basic Usage of Node.js

Versions at User Level

# install multiple node version
nvm install 10.13.0
nvm install 11.2.0
# choose current node version
nvm use 10.13.0

Versions at Project Level

mkdir new_project && cd new_project
echo '8.12.0' > .nvmrc
nvm use

Advanced: Build Requirements

Validating Node.js Platform

npm install -g bcrypt

Popular Packages

# build-task automation tools
npm install -g grunt
npm install -g gulp
npm install -g webpack
# web microframework
npm install express
# database ORM
npm install sequelize
# languages
npm install -g coffeescript
npm install -g typescript
# frontend framework
npm install -g @angular/cli

Example Server

mkdir -p hellojs && cd hellojscat <<-'PKG_MANIFEST' > package.json
{
"name": "express_hello_world",
"version": "1.0.1",
"description": "Express Hello World",
"author": "Joaquin Menchaca",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.13.3"
}
}
PKG_MANIFEST
cat <<-'SRVR_CODE' > server.js
'use strict';

const express = require('express');

// Constants
const PORT = process.env.EXPRESS_PORT || "8080";

// Application
const app = express();

// Multiple Route Scenario
app.get(['/', '/hello'], function (req, res) {
res.send('Hello world!\n');
});

// Dynamic Route Scenario
app.get('/hello/:username', function(req , res){
res.send("Why, Hello " + req.params.username + '!\n');
});

app.listen(PORT);
console.log('Running on http://localhost:' + PORT);
SRVR_CODE
npm install
npm start
curl -i localhost:8080
curl -i localhost:8080/Cindy
curl -i localhost:8080/Rachael

Conclusion

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, Kubernetes, CNI, IAC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store