-
Notifications
You must be signed in to change notification settings - Fork 19
Setup
This document is a quick guide to preparing for node.js Development.
See the node.js API and npm documentation.
Officially, it's pronounced "node dot js", but you may get some strange glances when saying that in real life.
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
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
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.
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"]
}]
}
}
We'll frequently use nodemon
to create a server daemon to watch our project files and auto-restart on changes and crashes.
-
Install
nodemon
:npm install -g nodemon
-
Use
nodemon
to start your server:nodemon -- index.js --foo=bar
Note: The
--
delimitsnodemon
arguments from arguments passed tonode
.
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
.
-
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
-
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
-
Virtual Studio Code (Free/FOSS)
-
JetBrains WebStorm ($50 after trial) (Features, Configuration)
-
JetBrains IntelliJ ($199 after trial) (Features, Configuration)
Follow the JetBrains Setup Guide to add node.js code completion and in-editor running and step-debugging. JetBrains also has excellent documentation. Google is your friend: WebStorm, IntelliJ
Sublime Text setup steps:
-
Download Sublime Text 3
-
Install Sublime Package Control
-
Install the following Sublime packages:
Note: For instruction on how to install packages, see the Sublime Package Control Basic Functionality Documentation
-
Install
babel-eslint
globally:# You likely have some of these installed already npm install -g eslint babel babel-eslint
- Atom (Text Editor, Free/FOSS) - Github's Hackable Editor
- Nodeclipse (Eclipse, IDE, Free)
There are some windows specific issues you may encounter. The following additional Windows-only setup steps will help you avoid them.
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:
- Install
ansicon
- Place
ansicon/x86/
oransicon/x64/
on yourPATH
- Start a
cmd.exe
by running theansicon
exe that you added to your path in the previous step. - If you are using
git-bash
, you will need to start yourgit-bash
session from youransicon
terminal by executinggit-bash-directory/sh.exe --login -i
.
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.
-
Install
babel
and create thebode
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 >
-
Create the
bodemon
alias (babel
+nodemon
):bodemonalias="alias bodemon='nodemon --exec babel-node -- -- '" echo $bodemonalias >> ~/.bash_profile # Persist alias bodemon somefile.js
-
To use
bode
andbodemon
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/