How to Set up Node.js in Visual Studio Code

0
403

Setting up Node.js in Visual Studio Code is straightforward. Here’s a step-by-step guide:


Step 1: Install Node.js

  1. Download Node.js from the official site: https://nodejs.org/
  2. Install it by following the setup wizard.
  3. Verify installation:
    • Open Command Prompt (Windows) or Terminal (Mac/Linux).
    • Run:
      node -v
      
      npm -v
      
    • If both return version numbers, Node.js and npm (Node Package Manager) are installed.

Step 2: Install Visual Studio Code

  1. Download and install VS Code from: https://code.visualstudio.com/
  2. Open VS Code.

Step 3: Install the Node.js Extension

  1. Open VS Code.
  2. Go to Extensions (Ctrl + Shift + X).
  3. Search for "Node.js Extension Pack".
  4. Click Install.

Step 4: Setup a Node.js Project

  1. Open a terminal in VS Code (Ctrl + `).
  2. Create a new project folder:
    mkdir my-node-app
    cd my-node-app
    
  3. Initialize a Node.js project:
    npm init -y
    
    This creates a package.json file.

Step 5: Create a Simple Node.js File

  1. Inside VS Code, create a new file: index.js.
  2. Add the following code:
    console.log("Hello, Node.js!");
    
  3. Run the file using the terminal:
    node index.js
    
    You should see:
    Hello, Node.js!
    

Step 6: Debugging in VS Code (Optional)

  1. Go to Run & Debug (Ctrl + Shift + D).
  2. Click "Create a launch.json file".
  3. Choose "Node.js".
  4. Set breakpoints and debug your code.