Setting up Node.js in Visual Studio Code is straightforward. Here’s a step-by-step guide:
Step 1: Install Node.js
- Download Node.js from the official site: https://nodejs.org/
- Install it by following the setup wizard.
- 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
- Download and install VS Code from: https://code.visualstudio.com/
- Open VS Code.
Step 3: Install the Node.js Extension
- Open VS Code.
- Go to Extensions (Ctrl + Shift + X).
- Search for "Node.js Extension Pack".
- Click Install.
Step 4: Setup a Node.js Project
- Open a terminal in VS Code (Ctrl + `).
- Create a new project folder:
mkdir my-node-app
cd my-node-app
- Initialize a Node.js project:
npm init -y
This creates a package.json
file.
Step 5: Create a Simple Node.js File
- Inside VS Code, create a new file:
index.js
.
- Add the following code:
console.log("Hello, Node.js!");
- Run the file using the terminal:
node index.js
You should see:
Hello, Node.js!
Step 6: Debugging in VS Code (Optional)
- Go to Run & Debug (
Ctrl + Shift + D
).
- Click "Create a launch.json file".
- Choose "Node.js".
- Set breakpoints and debug your code.