Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Developing Node.js with VS Code

Developing Node.js with VS Code

Visual Studio Code (VS Code for short) is a free, open-source code editor developed by Microsoft, and is currently one of the most popular editors for Node.js development.

Main advantages

Install VS Code

  1. Access the VS Code official website
  2. Download the version for your operating system
  3. Follow the installation wizard to complete the installation
  4. Complete VS Code tutorial:https://www.runoob.com/vscode/vscode-tutorial.html

In addition, domestic companies Alibaba and ByteDance also have AI IDEs developed based on VS Code:

Alibaba Qoder
An AI programming IDE launched by Alibaba, deeply customized based on VS Code
Visit ->
ByteDance Trae
A new-generation AI-native development environment launched by ByteDance
Visit ->

Recommended extensions

  1. Node.js Extension Pack: A collection package containing multiple Node.js-related plugins
  2. JavaScript (ES6) code snippets: Provides ES6+ code snippets
  3. Auto Rename Tag: Automatically renames HTML/XML tags
  4. ESLint: Code quality checking tool
  5. Prettier: Code formatting tool
  6. npm Intellisense: npm package auto-completion
  7. Path Intellisense: File path auto-completion

Create your first Node.js project

Initialize a project

  1. Open the terminal (press Ctrl+` in VS Code)
  2. Create a project folder and enter it:
    mkdir my-node-app
    cd my-node-app
  3. Initialize the npm project:

npm init -y

This will produce a package.json file:

Create the main file

  1. Create in VS Code app.js file
  2. Enter the following code:

Example

const http = require('http');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end(' RUNOOB Node Test ~ Hello, Node.js!\n');
});

const port = 3000;
server.listen(port, () => {
  console.log(`Server running at: http://localhost:${port}/`);
});

Run the Project

  1. Execute in the terminal:
    node app.js

  2. Open the browser and visit http://localhost:3000

Please note that when we type console., VS Code's IntelliSense can provide code suggestions:


Debug Node.js applications

VS Code provides powerful debugging features:

Configure debugging

  1. Click the "Run and Debug" icon in the left activity bar
  2. Click "Create a launch.json file"
  3. Select the "Node.js" environment

Set breakpoints

  1. Click to the left of the line number to set a breakpoint
  2. Press F5 to start debugging
  3. Use the debug toolbar to control the execution flow

Debug controls