Running Node.js

Now that Node is installed and configured, we're ready to start using it. However, before trying this, let's just verify that Node is installed correctly by printing its version number.

This book assumes that you're using the latest stable version of Node.js. If you have installed a different version, for example, by building from the latest unstable source code, then the examples in this chapter might not work. From the command line, you can run the following command to see which version you have installed on your system:

node --version

If the command is successful, the output will be Node's version number.

Note

At the time of writing this book, the latest stable release was Node v4.2.2.

Node provides a JavaScript runtime environment that can be accessed at any time by running the node command from your command-line or terminal window. This feature is called the CLI (command-line interface), and it allows you to experiment with Node. For the purpose of this book, you will be writing JavaScript source files instead of typing code through the CLI.

To run a JavaScript source file using the Node.js platform, run the node command passing the filename as a parameter as follows:

node filename.js

Now that you know how to run Node, let's put this into practice by running a simple command using the CLI. Open a terminal window and start Node.js:

node

This will start the CLI, which will then pause and wait for you to enter a JavaScript command. For instance, you can type the following command followed by ENTER:

console.log("Hello, World!!!");

This will print the string to screen. As you can see, the CLI is a great way to experiment with Node.js and is very useful, especially if you're new to JavaScript.

You can quit the CLI by typing Ctrl + D or Ctrl + C.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.113.208