Node.js

Installing Node on Linux

The following method of installing node involves using symbolic links to make both installing node and switching node versions easier.

On Debian systems, \usr\local is normally owned by root and belongs to the staff group which also has the set-group-ID bit set (see man chmod for more details on set-group-ID). This allows users belonging to that group to create files under that folder without needing to use su or sudo. On other systems you may need to use su or sudo with these instructions.

  1. On a Debian system, add the user doing the install to the 'staff' group. On Debian this can be done with the adduser command. On other systems see man useradd.

    $ sudo adduser myself staff
    

    You will need to logout and login again for the change to take effect.

  2. Untar the distribution, e.g.

    $ cd /usr/local/share
    $ tar -xvf ~/downloads/node-v4.4.4-linux-x86.tar.xz
    
  3. Create a symbolic link to the distribution, e.g.

    $ ln -s node-v4.4.4-linux-x86 node-current
    
  4. Create symbolic links to the relevant artifacts under the installation directory, e.g. /usr/local/share/node-v4.4.4-linux-x86

    $ cd  /usr/local/bin
    $ ln -s ../share/node-current/bin/node
    $ ln -s ../share/node-current/bin/npm
    $ cd /usr/local/include
    $ ln -s ../share/node-current/include/node/
    $ cd /usr/local/lib
    $ ln -s ../share/node-current/lib/node_modules/
    $ mkdir -p /usr/local/share/doc
    $ cd /usr/local/share/doc
    $ ln -s ../node-current/share/doc/node/
    $ mkdir -p /usr/local/share/man/man1/
    $ cd /usr/local/share/man/man1/
    $ ln -s ../../node-current/share/man/man1/node.1
    $ cd /usr/local/share/
    $ mkdir -p systemtap/tapset
    $ cd systemtap/tapset
    $ ln -s ../../node-current/share/systemtap/tapset/node.stp
    

Future installs can be done by simply untarring the distribution under /usr/local/share and updating the node-current symbolic link to point to the new version.

Installing on macOS

Node can be installed using MacPorts. E.g.

$ sudo port install nodejs6

Build Distribution Tar File

$ ./configure
$ make binary

Note: compiling Node on a Raspberry Pi 1 model B took over 9 hours.

Application Development

Getting Started

Documenting Your Code

npm

npm init creates some default scripts.

Installing packages

$ npm install PACKAGE-NAME --save

If the package is only required for development, use --save-dev instead of --save.

If the package is on GitHub, you can reference it as:

npm install <githubname>/<githubrepo>[#<commit-ish>]

See NPM cli for more detailed information.

Useful Packages

  • body-parser - Node.js body parsing middleware
  • bcrypt - Lib to help you hash passwords
  • Connect - An extensible HTTP server framework for node using "plugins" known as middleware
  • copyfiles
  • debug - A tiny node.js debugging utility modelled after node core's debugging technique
  • formidable - A Node.js module for parsing form data, especially file uploads
  • jsonwebtoken - An implementation of JSON Web Tokens
  • lodash - A modern JavaScript utility library delivering modularity, performance, & extras
  • Router - Simple middleware-style router
  • nconf - Hierarchical node.js configuration with files, environment variables, command-line arguments, and atomic object merging
  • node-autoquit - Automatically quit node.js servers when inactive
  • node-pool - Generic resource pool with Promise based API
  • node-postgres - Non-blocking PostgreSQL client for node.js
  • node-systemd - support for running node.js as a socket-activated service under systemd
  • node-uuid - Simple, fast generation of RFC4122 UUIDs.
    Note: Original implementation no longer maintained - https://github.com/broofa/node-uuid - see https://github.com/kelektiv/node-uuid/issues/142 for further details.
  • qs - A querystring parsing and stringifying library with some added security
  • sax-js - A sax-style parser for XML and HTML
  • serve-favicon - Node.js middleware for serving a favicon
  • ShellJS - Unix shell commands for Node.js
  • Turfjs - a JavaScript library for spatial analysis
  • xmlbuilder-js - An XML builder for node.js similar to java-xmlbuilder
  • UglifyJS2 - UglifyJS is a JavaScript parser, minifier, compressor or beautifier toolkit
  • winston - A multi-transport async logging library for node.js. "CHILL WINSTON! ... I put it in the logs."

Interesting Projects

  • Blob.js - implements the W3C Blob interface in browsers that do not natively support it
  • body-parser - Parse incoming request bodies in a middleware before your handlers, available under the req.body property
  • node-cssmin - A little node module that minimize CSS Files
  • dgeni - Flexible JavaScript documentation generator used by AngularJS, Protractor and other JS projects
  • electron - The Electron framework lets you write cross-platform desktop applications using JavaScript, HTML and CSS
  • qs - A querystring parsing and stringifying library with some added security
  • Rough.js - A light weight (~9kB gzipped) graphics library that lets you draw in a sketchy, hand-drawn-like, style
  • Speakeasy - a one-time passcode generator, ideal for use in two-factor authentication, that supports Google Authenticator and other two-factor devices

Installing Yarn

See https://yarnpkg.com/en/docs/install

Installing on macOS

Yarn can be installed using MacPorts. E.g.

$ sudo port install yarn

LTS schedule

https://github.com/nodejs/LTS

Uninstalling Node.js

On macOS, if Node.js was installed via a package, it can be removed by deleting the files listed in /var/db/receipts/org.nodejs.pkg.bom. The files can be listed by using the lsbom utility.

$ lsbom -f -l -s -pf /var/db/receipts/org.nodejs.node.pkg.bom

The file names are relative to the /usr/local/ directory.

Additionally, consider removing the following under /usr/local/:

  • /usr/local/lib/node_modules/
  • /usr/local/include/node/
  • /usr/local/share/doc/node/
  • /var/db/receipts/org.nodejs.node.pkg.*

See https://stackoverflow.com/questions/9044788/how-do-i-uninstall-nodejs-installed-from-pkg-mac-os-x for more information.

Build Tools

References

See also:

-- Frank Dean - 24 Mar 2016

Related Topics: AngularJS, DevelopmentSetup, LinuxDevelopment