Node.js

Installing Node on Linux

See https://github.com/nodejs/help/wiki/Installation

If you change the installed folder to be only writable by root, install modules globally using sudo as follows, avoiding attempting to follow symlinks:

$ sudo PATH=$PATH -E \
/usr/local/lib/nodejs/node-$NODE_VERSION-linux-x64/lib/node_modules/npm/bin/npm-cli.js \
install -g yarn

Installing on macOS

Node can be installed using MacPorts. E.g.

$ sudo port install nodejs12

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-2fa - Easy 2-Factor Integration For Node.JS
  • 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.
  • nodemon - Monitor for any changes in your node.js application and automatically restart the server
  • qs - A querystring parsing and stringifying library with some added security
  • sax-js - A sax-style parser for XML and HTML
  • sendmail - Send mail without SMTP server
  • serve-favicon - Node.js middleware for serving a favicon
  • ShellJS - Unix shell commands for Node.js
  • smtp-protocol Write smtp clients and servers
  • 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

Building a tarball package

$ yarn pack

https://yarnpkg.com/cli/pack

Using Yarn Offline

https://classic.yarnpkg.com/blog/2016/11/24/offline-mirror/

Yarn Selective Dependency Resolution

https://classic.yarnpkg.com/en/docs/selective-version-resolutions/

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:

Resources

-- Frank Dean - 24 Mar 2016

Related Topics: AngularJS, DevelopmentSetup, DockerTips, LinuxDevelopment