Getting started with js2

16 / Feb / 2013 by Amit Kumar 0 comments

We are assuming that you have installed node.js in your system, in case if you have not installed node.js, first install node.js.
To install js2 run the command given below:

npm install js2

Let’s create a simple Hello World project, but first let’s create a project directory structure:

mkdir helloWorldJs2
cd helloWorldJs2
mkdir scr lib

Now create a HelloWorld.js2 file in scr directory with the content given below:

class HelloWorld {
  function sayHelloWorld() {
    console.log("Hello World");
  }
}

To compile js2 file into js file in lib directory run the command given below:

js2-node compile -f=node ./src ./lib

Above command will compile HellowWorld.js2 file and will generate HelloWorld.js file in lib directory. Now create a js script named as hello.js in scr directory with following content:

var HelloWorld = require('../lib/HelloWorld').HelloWorld;
var helloWorld = new HelloWorld();
helloWorld.sayHelloWorld();

Now run hello.js file with the command given below, And you will get Hello World printed at console.

node src/hello.js

Amit Kumar
amit.kumar@intelligrape.com
in.linkedin.com/in/amitkumar0110
twitter.com/amit_kumar0110
More Blogs by Me

FOUND THIS USEFUL? SHARE IT

Leave a comment -