Codepath

Setup

Overview

This document is a quick guide to preparing for node.js Development.

Documentation

See the node.js API and npm documentation.

Download Node.js

Officially, it's pronounced "node dot js", but you may get some strange glances when saying that in real life.

Which Version?

Typically you'll use the latest node.js LTS release (even numbered version) due to increased stability. Alternatively, the latest "Current" release contains the latest features and V8 (more information here). Both can be found at nodejs.org.

A Note on Semver:

Semver versioning uses a 3 number hierarchy, major.minor.patch (e.g., 2.1.4):

  • major - backwards breaking
  • minor - new functionality
  • patch - bug fixes

New even-numbered major releases enjoy a 6 month lead-time before being marked LTS. This allows npm package authors time to update. Because of this, for production development, the current LTS release is recommended.

Pre 1.0

0.*.* releases followed an even/odd versioning scheme:

  • Odd minors (0.9.*) - unstable (often breaking) branch development
  • Even minors (0.12.*) - stable (backwards compatible) branch development

Recommended Installation Path: nvm

NVM supports nearly every version of node.js and enables both quick transitions between versions and switching on a session basis.

# Install nvm
curl https://raw.githubusercontent.com/creationix/nvm/latest/install.sh | bash
nvm install stable          # Install latest stable version
nvm alias default stable    # Set as version to load by default in the future

Alternative Installation Paths:

Download from nodejs.org

Verify npm Install

Note: Throughout this setup we'll be using -g to install packages. For an explanation, see global vs installed packages. (Summary: -g packages are executable in the terminal, local packages can be loaded programmatically with require())

# npm is updated more frequently than node
# So, let's install the latest npm
npm install -g npm@latest    

# If that fails for permission reasons, you have 2 options:
# Option 1: chown the install directory e.g. /usr/local
sudo chown -R $(whoami) `npm config get prefix`

# Option 2, Step 1: Set the npm install path to a custom directory
mkdir ~/.npmprefix && npm config set prefix ~/.npmprefix

# Option 2, Step 2a: Persist the custom npm install directory
npmpath='export PATH=`npm config get prefix`/bin:$PATH'

# Option 2, Step 2b: Add to your executable path
# OSX
echo $npmpath >> ~/.bash_profile && source ~/.bash_profile
# Linux
echo $npmpath >> ~/.bashrc && source ~/.bashrc
See the npm documentation on fixing permissions for a more comprehensive explanation of the above.

Install ESLint

To avoid common mistakes, we'll use eslint to check our code while developing.

npm install -g eslint@1.x babel-eslint

eslint can be run directly, but more often will be utilized through IDE or editor integration.

We'll configure eslint with an .eslintrc file at the root of our project and fallback to ~/.eslintrc. We recommended the following .eslintrc:

{
  "env": {
    "node": true,
    "es6": true
  },
  "parser": "babel-eslint",
  "rules": {
    "curly": [2, "multi-line"],
    "no-throw-literal": 1,
    "strict": [2, "never"],
    "semi": [2, "never"],
    "quotes": [2, "single"],
    "no-var": 1,
    "eol-last": 1,
    "no-new-require": 1,
    "no-sync": 1,
    "no-mixed-requires": [1, false],
    "comma-dangle": 0,
    "new-cap": [2, {
      "capIsNewExceptions": ["Schema"]
    }]
  }
}

Install the nodemon daemon & npm-do helper script

nodemon

We'll frequently use nodemon to create a server daemon to watch our project files and auto-restart on changes and crashes.

  1. Install nodemon:

    npm install -g nodemon
  2. Use nodemon to start your server:

    nodemon -- index.js --foo=bar

    Note: The -- delimits nodemon arguments from arguments passed to node.

npm-do

In node.js, the convention is to prefer locally installed tools. For example, every project, Assignment and Exercise will have its own locally installed and versioned copy of nodemon.

  1. Install the npm-do helper script to run local dependency executables:

    Note: On Linux, replace .bash_profile with .bashrc below.

    npmdo='function npm-do { (PATH=$(npm bin):$PATH; eval $@;) }'
    echo $npmdo >> ~/.bash_profile                     # Persist alias
    source ~/.bash_profile                             # Load in current shell
  2. Run executables from the nearest node_modules/.bin:

    npm-do nodemon                      # ./node_modules/.bin/nodemon
    npm-do babel-node index.js          # ./node_modules/.bin/babel-node index.js
    npm-do babel-node index.js          # `npm bin`/babel-node index.js

Install an Editor

Recommended: JetBrains IntelliJ/WebStorm (IDE) or VSCode (Text Editor)

Alternative: Sublime Text (Text Editor)

Sublime Text setup steps:

  1. Download Sublime Text 3
  2. Install Sublime Package Control
  3. Install the following Sublime packages:

    Note: For instruction on how to install packages, see the Sublime Package Control Basic Functionality Documentation

  4. Install babel-eslint globally:

    # You likely have some of these installed already
    npm install -g eslint babel babel-eslint

Honorable metions

  • Atom (Text Editor, Free/FOSS) - Github's Hackable Editor
  • Nodeclipse (Eclipse, IDE, Free)

Windows

There are some windows specific issues you may encounter. The following additional Windows-only setup steps will help you avoid them.

ANSI escape codes

cmd.exe's lack of support for ANSI escape codes (text coloring in the terminal). Since babel-node uses these by default, and does not have an easy way to turn them off, we'll need to add ANSI support to make our babel-node errors readable:

ANSI Encoding Errors

  1. Install ansicon
  2. Place ansicon/x86/ or ansicon/x64/ on your PATH
  3. Start a cmd.exe by running the ansicon exe that you added to your path in the previous step.
  4. If you are using git-bash, you will need to start your git-bash session from your ansicon terminal by executing git-bash-directory/sh.exe --login -i.

Optional Advanced Setup

Install the Babel ESNext Transpiler

The new JavaScript language features in ESNext will eventually make it into V8, on which node.js runs. Rather than wait for V8 to support ESNext, we can today use Babel to transpile ESNext code into a JavaScript version currently supported by node.js.

Note: We'll be using the bode and bodemon aliases in place of node and nodemon throughout the course.

  1. Install babel and create the bode alias:

    npm install -g babel-cli                           # babel-node executable
    bodealias="alias bode='babel-node -- '"            # Alias babel-node to bode
    echo $bodealias >> ~/.bash_profile                 # Persist alias
    source ~/.bash_profile                             # Load in current shell
    bode                                               # Use ESNext today
    >
  2. Create the bodemon alias (babel + nodemon):

    bodemonalias="alias bodemon='nodemon --exec babel-node -- -- '"
    echo $bodemonalias >> ~/.bash_profile                # Persist alias
    bodemon somefile.js
  3. To use bode and bodemon with IDEs, create executables instead of aliases:

    # Create a consistent location for bode and bodemon
    mkdir ~/bin
    echo 'exec bode "$@"' > ~/bin/bode
    echo 'exec bodemon "$@"' > ~/bin/bodemon
    chmod -R +x ~/bin/
Fork me on GitHub