Lets create and start to make a simple node.js application with the following steps. For example, lets create this application MyApp

1. Create a folder where you want to create your application called MyApp
$ mkdir myapp
$ cd myapp

2. Open the folder with Vs Code or your favorite IDE. I will be using VS CODE for this tutorial.
$ code .

3. You should have an empty project with no folders or fildes. Open the terminal in VS CODE (Control + `)

4. Create an empt file called app.js
$ touch app.js

4. Initialize the project with this npm command:
$ npm init -y

If you leave out the -y option, you will need to populate the following fields:

package name: (example1)myapp
version: (1.0.0)
description: Just a very basic nodejs app
entry point: (app.js)
test command:
git repository:
keywords:
author:
license: (ISC)

Is this OK? (yes) yes

5. Now you should have a file called package.json, open this file and add and add start to "scripts": {  - The start object property should now look like this:

  "scripts": {
    "test""echo \"Error: no test specified\" && exit 1",
    "start""node app.js"
  },

5. Add the following code to app.js

console.log("hello world");

6. Now you can run the script in your console using Node.js with the following command:

$ node app.js

Done. BUT if you want to take a step further, run it as a server with auto refresh to render HTML, follow the next steps:

7. Install Nodemon to make your application automatically refresh when you make a change

$ npm i nodemon or (npm i --save-dev nodemon)

8. Change scripts in packages.json

FROM:

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },

 

TO:

  "scripts": {
    "devStart": "nodemon App.js"
  },

 

9. Start your development server with on App.js

$ npm run devStart

 10. Open App.js and enter the following code:

console.log("Hello World");

 

You will see Hello World in the terminal.


[nodemon] clean exit - waiting for changes before restart
[nodemon] restarting due to changes...
[nodemon] starting `node App.js`
Hello World
[nodemon] clean exit - waiting for changes before restart

 

Done.