When using VS Code to write Node.js code, you need to install the Node.js environment and VS Code extension first. Here are the steps on how to run Node.js code in VS Code:
If you haven’t installed the Node.js environment yet, please install it in Download and install Node.js from the official website. After the installation is complete, you can verify whether the Node.js version is correct by running the following command:
node -v
In order to better use VS Code to write Node.js code requires the installation of some related extensions. You can search for "Node.js" in VS Code's plug-in store to find related extensions, as shown in the following figure:
After the installation is complete, restart VS Code.
In VS Code, you can create a Node.js project through the terminal or command line. Open VS Code, press the "ctrl shift p" key to open the command panel, and search for "Terminal: Create New Integrated Terminal" in the panel. Selecting this command will open a terminal window in VS Code:
In the terminal panel, enter the following command to create a new Node.js project:
mkdir my_project cd my_project npm init
After performing the above operations, a project named " my_project" folder and create a file named "package.json" in it to manage project dependencies. During this process, fill in the project information as needed.
In VS Code, you can use the built-in editor to write Node.js code. Create a file named "index.js" and write the following code in the file:
console.log("Hello World!");
After saving the file, you can open the terminal panel in VS Code and use the following command to print out "Hello World" in the console !" Message:
node index.js
VS Code also provides a built-in debugger that can help you debug Node.js code. You can use the following steps to debug Node.js code:
Step 1: Add configuration in the "launch.json" file
In the sidebar of VS Code, find the debug view, and Find the "launch.json" file there. Add the following configuration to the file:
{ "version": "0.2.0", "configurations": [{ "type": "node", "request": "launch", "name": "Launch Program", "program": "${file}" }] }
After saving the file, the configuration will be automatically saved, and this newly created configuration item can be seen in the drop-down menu above the "Debug" view.
Step 2: Insert a breakpoint in the code
Open the "index.js" file in the editor and add a breakpoint in the code. Breakpoints can be set by clicking on the line number on the left side of the editor.
Step 3: Start the debugger
Press the F5 key or click the "Run" button to start the debugger.
Step 4: Debugging the code
When the debugger starts and encounters a breakpoint, you can use commands such as single-step execution and resume execution to view the execution of the code. You can use the variables, stack, or console views on the debug tool window to view code status and debugging information.
Summary
The above are the steps to run Node.js code in VS Code. Excellent development tools can greatly improve program development efficiency. It is very important for developers to understand how to quickly run and debug Node.js code in VS Code. Hope this article is helpful to you, thanks for reading.
The above is the detailed content of How vscode runs nodejs code. For more information, please follow other related articles on the PHP Chinese website!