Hello World: My First Node Application

DZone 's Guide to

Hello World: My First Node Application

In this article, we go over the basic commands you need to know to get your Node.js backend environment set up and running simple actions.

· Web Dev Zone ·
Free Resource

Writing Your First Node Application:

I'm using Sublime Text as my text editor. You can also use Sublime Text for your Node application development.

Open your text editor, copy the following line in the file and save the file as helloworld.js.

console.log("hello world"); 

You can run the file using the following command :
node helloworld.js  
Your output should be: hello world

Bingo! You successfully ran your first Node code!

Question: Hey you didn't use npm anywhere? Why?

npm is mainly used to maintain the dependencies of Node projects and in the above example, I didn't use any dependent module. In the following example, we will start using npm.

Question: So it doesn't need any external JavaScript files?

Correct, we won't need to include any external JavaScript files to use Node modules.

The above example is something similar to writing one Java class and running it using the command line. Next, we are going to see how to create a Node application.

Create a Node Project

You can create a new Node project by using the following command:

npm init 

First, create one folder and in the command prompt go to that folder. Then run the command.

Now you will be asked series of questions. Answer all the questions and then click yes.

name: (nodeapp) 
version: (1.0.0) 
description: My node app
entry point: (index.js) 
test command: 
git repository: 

license: (ISC)

Name: This will be the name of the application, you are writing. Everything should be in lowercase letters, with no spaces between words, but you can use dashes/underscores in between.

Version: This denotes the version of the application. Every major and minor release version needs to be updated. The version should follow semantic versioning specifications.

Description: Description of the application.

Entry Point: The Starting point of the application.

Test Command: You can enter the test command, which in turn invokes the unit test cases.

Git Repository: You can mention the git repository of your project.

Keywords: You can mention the keywords related your application. So during a search your Node module will be displayed. This will be discussed in detail later.

Author: Your name and email id should be mentioned in this field.

License: You can mention the license details of the application.

Once the above step is completed, you can find the package.json file created in your application folder. If you want to modify the above content you can change in that file.

Otherwise, you can also modify the content you can use with the npm set command:

npm set init.license "MIT" 

The above command sets the license value present in the package.json to MIT. You can do this for all other parameters.

You have successfully created your first node application!

We will put some code into our node application in our next article.

Please share your comments or questions in the below comment section!

javascript, node.js, npm, web dev

Published at DZone with permission of Raghuraman Kesavan , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}