Creating New Project in NodeJs

Complete beginner guide to get started with NodeJs.

  1. Create a new project folder in your file directory and name it node_basic

  2. Open that project folder in VS Code (recommended Editor). To open the project folder you can do either of the following ways.

    • Go inside project folder. Click right click outside file and folder holding 'Left Shift Key' and click on 'Open Powershell window here' or 'Open Linux shell here' or 'Git Bash here' depending on shell you have. Inside the shell in project directory, give command as shown below to open projects in VS Code.

      code . 
      
    • Simply, drag and drop project folder in VS Code open the project.

  3. Till Now, no any files and folders are created. We will created necessary file and folders to start the project. Before that make sure that you already had installed latest stable version of Node.

    • You can check whether node installed with command below in the terminal
    node //if installed, shows Welcome to NodeJs message
    

    In Run (window) open terminal of nodeJs typing "node.exe" in the box.

    In the terminal check Node Version.

    node -v 
    

    Exit from Node CLI

    process.exit()
    

    or Ctrl+D or Ctrl+C(twice) or type ".exist".

    Now, lets get back to VS Code and in terminal there inside project folder. Begin node project with the code:

    npm init -y
    

    'npm init' command allows to set package naeme, version, description and other information about application. Whereas 'npm init -y' allows to take default values and save. npm instand for 'Node Package Manager' while '-y' stands for yes.

    This creates new file package.json.

  4. Create a index script file and name it either 'app.js' or 'index.js' in project root directory (outside of all folders) (* no any folder should exist till now in project folder).

  5. Index file is referred as index.js file here after. Add console.log a line of code in index.js and save it and run the code. The code is:

    console.log("Test");
    

    Run the code saving the file. In the terminal type code: node index.js

    node index.js
    

    Upon running it prints Test.