Switching node versions with Homebrew


Just a quick trick to switch between node versions when you install more than one version through Homebrew (brew install node)

You check the currently selected version with node --version. Working out version from paths in brew list node or looking for an asterisk next to a version listed in brew info node can also be used if you're after a different package.

1) List the versions present on your system: brew list --versions node

2) Switch to one of the versions, in my case I'm switching to 6.2.2: brew switch node 6.2.2

That's it, you can verify the operation was successful with node --version.

Tags: brew, homebrew, node, versions

Builds failing because of node-gyp missing common.gypi


This issue became apparent on one of CI build boxes. For whatever reason the common.gypi file has gone missing. Stack trace: gyp: /home/builder/.node-gyp/5.9.1/common.gypi not found (cwd: /home/builder/buildtmp/X-JOB1/app/node_modules/fsevents) while reading includes of binding.gyp while trying to load binding.gyp gyp ERR! configure error gyp ERR! stack Error: `gyp` failed with exit code: 1 gyp ERR! stack at ChildProcess.onCpExit (/usr/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:355:16) gyp ERR! stack at emitTwo (events.js:100:13) gyp ERR! stack at ChildProcess.emit (events.js:185:7) gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:204:12) gyp ERR! System Linux 3.10.0-229.7.2.el7.x86_64 gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--module=/home/builder/buildtmp/X-JOB1/app/node_modules/fsevents/lib/binding/Release/node-v47-linux-x64/fse.node"... more

Tags: node, node-gyp, common.gypi, npm, js

Fixing EMFILE error


If you're on a Mac like me and you develop sizeable Node.js applications you will at some point suffer from the issue which manifests itself in something like this:

EMFILE, too many open files /app/node_modules/random-module/index.js

The problem behind this is the Mac's unusually low open file descriptors limit (256).

To change this simply enter this command in terminal and your issue will be gone:

ulimit -n 1024

Then simply run your app in the same terminal session. You may need to increase the limit even further depending on the size and complexity of your application.

It's also worth checking out this thread on Stack Exchange about persisting ulimit settings.

Tags: node, error, troubleshooting

Unit testing of Node.js with Mocha and Istanbul


Useful tips

Basic usage

./node_modules/.bin/_mocha --reporter spec

This command will help you run just a subset of tests in a given directory.

./node_modules/.bin/_mocha recursive test/my-subdir

Quick coverage check

./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha

Tags: node

Publishing node modules to a private repository


This is a tutorial on how to use your own private repository to publish node modules to. In most cases this will be a CouchDB based proxy of the public npm registry like Sinopia. I assume that is already set up and all you need is to configure your local development machine to use that registry. Publishing your first package 1) Set your npm config to point to your private repo. npm set registry http://your-private-repo:4873/ npm set always-auth true npm adduser The last command (adduser) will prompt you for the username, password and email. 2) Go to the location of files you want to be included in your package and make sure you have a valid package.json there. You may also add .npmignore file to exclude some files from being part of your package (i.e. unit tests). node_modules folder is automatically ignored but it won’t hurt to add it just in case. Remember to add the publishConfig property. 3) (optional) If you’re publishing the first version of your package, use npm pack to... more

Tags: node, publish, private