Developing Node.js with VS Code

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

Key Advantages

  • Built-in Node.js support: provides features such as intelligent code completion and debugging
  • Rich extensions: various Node.js development tools can be installed via the extension marketplace
  • Integrated terminal: run Node.js programs directly in the editor
  • Lightweight and fast: fast startup speed and low resource usage

Installing VS Code

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

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

Recommended Extensions

  1. Node.js Extension Pack: a collection pack 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: automatic completion for npm packages
  7. Path Intellisense: automatic completion for file paths

Creating Your First Node.js Project

Initializing the 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 an npm project:

    npm init -y

    This will generate a package.json file:

Creating the Main File

  1. In VS Code, createapp.jsthe 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(' EXAMPLE Node Test ~ Hello, Node.js!\n');
    });

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

Running the Project

  1. Execute in the terminal:
    node app.js

  2. Open a browser and visithttp://localhost:3000

Note that when we typeconsole., VS Code's IntelliSense can provide code suggestions:


Debugging Node.js Applications

VS Code provides powerful debugging features:

Configuring 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

Setting Breakpoints

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

Debug Controls

  • Continue (F5): continue execution to the next breakpoint
  • Step Over (F10): execute the current line without stepping into functions
  • Step Into (F11): step into the function body
  • Restart (Ctrl+Shift+F5): restart debugging
  • Stop (Shift+F5): end debugging
Other Extensions