nodejs

Ways to output text in node scripts

Snippet

Different ways to output text in node scripts.

Of note, console.log() calls process.stdout.write with formatted output.

process.stdout.write("look mom, no trailing newline");
 
console.log("this will have a newline at the end");

Exit from a node script

Snippet

Node scripts wait for the last event before exiting. Use the global process object's exit method to exit cleanly.

Ends the process with the specified code. If omitted, exit uses the 'success' code 0.

Use a positive (read: non-zero) exit code to indicate error. 1 is reasonable. The shell that executed node should see the exit code as 1.

// default to 0, success
process.exit()  
 
// actual call
process.exit([code])
 
/* to exit with a 'failure' code: */
process.exit(1);

Upgrade Node.js via NPM and n

Snippet

Upgrade local npm via the command line using n

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

Fix "npm ERR! Error: No compatible version found:" errors by upgrading

Blog

Fix "ERR! Error: No compatible version found: browser-sync-client@'^0.1.7'" errors with npm by upgrading your node version.

Barfs:

$ node --version 
v0.10.9

Works:

bash-3.2$ node --version 
v0.10.28
npm http 200 https://registry.npmjs.org/browser-sync-control-panel/0.0.5
npm http GET https://registry.npmjs.org/commander/-/commander-2.1.0.tgz
npm ERR! Error: No compatible version found: browser-sync-client@'^0.1.7'
npm ERR! Valid install targets:
npm ERR! ["0.0.2","0.0.3","0.0.4","0.0.5","0.1.0","0.1.1","0.1.2","0.1.3","0.1.4","0.1.5","0.1.6","0.1.7","0.1.8"]
npm ERR!     at installTargetsError (/usr/local/lib/node_modules/npm/lib/cache.js:709:10)
npm ERR!     at /usr/local/lib/node_modules/npm/lib/cache.js:631:10
npm ERR!     at saved (/usr/local/lib/node_modules/npm/node_modules/npm-registry-client/lib/get.js:138:7)
npm ERR!     at Object.oncomplete (fs.js:107:15)
npm ERR! If you need help, you may report this log at:

Also recommended:

$ npm cache clear
$ npm rebuild