Hello JavaScript with Node.js

JavaScript is a popular web language initially implemented on Netscape Navigator and later adapted on virtually all browsers. Since a JavaScript engine is a part of a browser, we had to embed JavaScript file in a HTML page, to load the script from the browser, and to use function like prompt() or alert() to receive from and response to developers. Nevertheless, with Node.js, we can program in JavaScript like in Perl, meaning running JavaScript file from the command line.

Node.js is a platform built on Chrome’s JavaScript runtime for building network applications. We can use the interpreter of Node.js to practice JavaScript. To install Node.js, go to its website and download the binary distribution of Node.js. If you are using Linux, you can likely get Node.js from system package manager. After installation, you obtain node, the JavaScript interpreter of Node.js and a package manager, npm, of Node.js.

We start with a “Hello world” program. Save the following content in hello.js or other file name.

#!/usr/bin/env node
"use strict";
console.log("Hello World!");

You may notice that we invoke strict mode here. Strict mode change some semantics of JavaScript, making your script less error-prone. However, the behaviors after invoking strict mode vary in different browsers. For compatibility, it’s better to wrap strict mode inside a function. You may see more infomation at this page from Mozilla.

Execute the script with node or add exec mode with chmod.

$ node hello.js
Hello World!

Or do this:

$ chmod +x hello.js
$ ./hello.js

If you want to install third-party libraries of Node.js, use npm. However, npm behaves a little different from the package manager of other programming language. npm install libraries into the subdirectory of current directory called node_modules and node search node_modules for libraries. If you change to different directories, you may need to re-install libraries. To install libraries to a fixed system directory, install Node.js packages with npm install -g.

You also need to set NODE_PATH variable in shell config file like ~/.profile.

export NODE_PATH=/usr/local/lib/node_modules

If you begin learning JavaScript with Node.js, you may notice that there is no built-in function in Node.js to receive input from user. If you want to get input data from user and don’t mind blocking I/O, consider readline-sync. To install readline-sync with npm:

$ npm install -g readline-sync

Here is an example to use readline-sync

var readlineSync = require('readline-sync');
var answer = readlineSync.question('What is your favorite food? ');
console.log('Oh, so your favorite food is ' + answer);

Using blocking I/O is not a good style in Node.js, but it’s easier than writing callbacks just for inputs from yourself.

Node.js store command line arguments in process.argv array. To receive data from command line arguments, do like the following example:

var arg = process.argv.slice(2);
// change console.log to other intended description

We do no command line parsing here. We just receive data from first argument. If you need some command line parsing, you can use optimist.

After knowing some basic I/O, you may start learning JavaScript syntax. A good online tutorial like Eloquent JavaScript will be a good starter.

上篇Add Bookmarks with Coherent PDF Tools
下篇Convert TXT to Excel with Perl