Making a simple Node.js hello world in the command line

Let's start with a simple Hello world. We will just log a message to the console.

First, we need to create a Node.js workspace, and this is done by executing the following in the code destination folder:

    npm init 

You will have to provide some further information on the module you are about to create. If you prefer npm populating the default values for you, just issue the following command:

    npm init -f  

If you choose the -f parameter, it will create a package and force the default settings. The package name should be taken from the folder name the package is created within.

At this stage, our Node.js package has been created, so let's have a look at the package.json. Some basic information on the package can be found inside this file, as well as the package entry point, which in our case is the default index.js.

We can now start writing the code. Let's create an index.js file so we have a placeholder for the code we are about to write. In this example, the code will be very simple indeed:

console.warn('Hello world!'); 

Once the file has been saved, we can execute it. In order to do so, simply type the following in the console:

    node index.js 

The output should not really surprise us:

    Hello world!  
The source code for this example is available in the chapter's resources in the code/01_hello_world directory.
..................Content has been hidden....................

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