Skip to content

Node JS

Andrew Colbeck edited this page Feb 28, 2019 · 14 revisions

Updating Node and NPM to latest versions

Command Action Source
npm view node versions Shows all latest node versions
node -v Shows the current version of node being used
sudo npm install npm@latest -g Installs latest stable version of npm src
sudo nvm install node --latest-npm --reinstall-packages-from=node Installs latest stable version of node and all previous packages src

Change Version of Node currently in use

Command Action Source
nvm i 10.15.1 Installs Node version 10.15.1
nvm use 10.15.1 Changes version of Node that is currently in use to 10.15.1



Creating a Node Application

Command Action
npm -v and node -v Check versions of npm/node, make sure same for all collaborators
mkdir APP-NAME make the directory
touch .gitignore Add gitignore file, populate file
npm init OR npm init --yes Creates Package.json file, add name/version/details or skip with second command
npm i PACKAGENAME Install Packages, common ones include express, joi
$nodemon Start Node Monitor



Installing Packages and Dependencies

Command Action
npm i Install all dependencies (found in Package.JSON file)
npm i PACKAGENAME Installs Package (no need for --save if using latest npm ver.)
npm i PACKAGENAME@1.1.1 Installs a specific version of a package
npm list --depth=0 Show Versions of Packages that are currently installed (take off --depth for full dependencies)
`npm un PACKAGENAME Uninstalls the specified Package



Development Tools

Command Action
sudo npm i jshint --save-dev Installs a Java Syntax checking Tool
`sudo npm i -g nodemon Installs a Node Monitoring Tool which restarts the server when a change is made



Updating Package Versions

Command Action
npm outdated Checks all packages within project and shows latest versions vs installed versions
npm -g outdated Checks all global packages
npm view PACKAGENAME versions Shows all available versions of a package
npm update Updates to 'Wanted' versions that correspond to ~ or ^ (non-breaking versions)
npm-check-updates Uses a module to show all available updates
ncu -u WARNING! Updates to the latest versions of packages and may break code



Installing Node and NPM

For Mac

Command Action Src
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh|bash Installs the Node Version Manager (nvm) version of Node.js, so that you can roll back if required when working on older projects. src
nvm --version Display all node versions
nvm i 7.2.0 This would install node version 7.2.0
sudo npm i -g npm-check-updates Installs a module that is used to check for Wanted/Latest package versions



How to Run

Download/Clone Repo, open with Editor and type the following into terminal/console: npm run dev

Install all required packages with the following command: npm install

Patterns

Include a built-in MODULE from Node (see http://nodejs.org and click API documentation for avail. modules)
const fs = require('fs'); // FILE SYSTEM

Include a CUSTOM MODULE / CLASS / FUNCTION
const Logger = require('./logger');
const logger = new Logger(); // Initialises to a new Obj

Example Function
function sayHello(name){
  console.log('Hello ' + name);
}

Example function call
sayHello('Andrew');

Show a module's contents in Console
console.log(logger);

Store function call results into variables
var pathObj = path.parse(__filename);
var totalMemory = os.totalmem();
var freeMemory = os.freemem();

Concatinate a string with a variable and print to console console.log('Total Memory =' + totalMemory);

The V8 Engine inside Node contains a lot of the Template Strings from ES6 / ES2015: ECMAScript 6
Example of a Template string which doesn't require concatination
console.log(`Free Memory: ${freeMemory}`);

SYNCHRONOUSLY copy entire Folder contents into Files constant
const files = fs.readdirSync('./');
console.log(files);

ASYNCHRONOUSLY copy the entire Folder contents into Files constant
fs.readdir('./', function(err, files){
  if (err) console.log('Error', err);
  else console.log('Result', files);
});

Clone this wiki locally