From David's Wiki
Revision as of 08:04, 28 October 2019 by David (talk | contribs)
Jump to navigation Jump to search
\( \newcommand{\P}[]{\unicode{xB6}} \newcommand{\AA}[]{\unicode{x212B}} \newcommand{\empty}[]{\emptyset} \newcommand{\O}[]{\emptyset} \newcommand{\Alpha}[]{Α} \newcommand{\Beta}[]{Β} \newcommand{\Epsilon}[]{Ε} \newcommand{\Iota}[]{Ι} \newcommand{\Kappa}[]{Κ} \newcommand{\Rho}[]{Ρ} \newcommand{\Tau}[]{Τ} \newcommand{\Zeta}[]{Ζ} \newcommand{\Mu}[]{\unicode{x039C}} \newcommand{\Chi}[]{Χ} \newcommand{\Eta}[]{\unicode{x0397}} \newcommand{\Nu}[]{\unicode{x039D}} \newcommand{\Omicron}[]{\unicode{x039F}} \DeclareMathOperator{\sgn}{sgn} \def\oiint{\mathop{\vcenter{\mathchoice{\huge\unicode{x222F}\,}{\unicode{x222F}}{\unicode{x222F}}{\unicode{x222F}}}\,}\nolimits} \def\oiiint{\mathop{\vcenter{\mathchoice{\huge\unicode{x2230}\,}{\unicode{x2230}}{\unicode{x2230}}{\unicode{x2230}}}\,}\nolimits} \)

Node.js is a JavaScript environment based on Chrome's V8 JavaScript engine. Node Package Manager (npm) is used to manage node packages installed on your computer or used in your node projects.



Just download the latest LTS release from the Node.js website


Using the Node Version Manager (nvm) is recommended. This does not require sudo as nvm is installed in your home directory. You should never use sudo when using npm. Doing so could lead to permission issues.

curl -o- | bash
# Reload .bashrc
source ~/.bashrc
nvm install --lts node
nvm install-latest-npm


JavaScript files can be run directly using node index.js

Filesystem Access

Install fs-extra to your project.

const fs = require('fs-extra');
const path = require('path');

// Synchronous functions

//// Only in fs-extra
// Remove file or folder (rm -rf)
// Does nothing if path doesn't exist

// Creates a directory if it doesn't exist
// This example will create folder1 and folder2 if they don't exist

//// Available in node's native 'fs'
// List all files and folders in a directory

// Ensure file exists
try {
  fs.accessSync('etc/passwd', fs.constants.R_OK | fs.constants.W_OK);
  console.log('can read/write');
} catch (err) {
  console.error('no access!');

// Read an entire text file as a string
fs.readFileSync('temp.txt', 'utf8');


Mostly the same as web JavaScript.

console.log("a", "b");

console.error("to stderr");


Install csv from npm.

Running other processes

You can invoke other processes or CLI programs using spawn or spawnSync

const {spawn} = require('child_process');

Package Management


To add node packages to an new project. Use npm init
To download the dependencies for a node project, run npm install or npm ci.
npm install installs the latest dependencies listed in package.json subject to the requirements listed in that file. It also updates package-lock.json.
npm ci installs exact versions from the package-lock.json.


Yarn is an alternative to npm. It caches packages on disk to speed up installs. While you can migrate from npm to yarn, it is recommended to stick to a single package manager since they each use their own lockfile.


pnpm uses symlinks to a single copy of each dependency to save disk space. pnpm uses its own lockfile instead of packages-lock.json.

Useful Packages


fs-extra adds additional commands for accessing the filesystem such as copying and moving files.