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.
You can run the file using the following command :
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.
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:
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: keywords: author: 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!