node.js基于VSCode的开发环境的搭建非常简单。
说明:本文的前置条件是已安装好node.js(具体安装不再赘述,如有需要可评论区留言)。
阅读本文可掌握:
- 方便地进行js单步调试;
- 方便地查看内置的对象或属性;
安装插件
-
Code Runner(可选)
-
JavaScript(ES6) code snippets

hello world
新建一个workspace,编写一个简单的hello world代码,使用vscode来运行调试。

此时,点击"F5"运行,会弹出提示框,找不到解释器。

根据提示,只需要保证PATH环境变量有node的可执行程序即可。也可以在launch.json中配置"runtimeExecutable"。
launch.json
简单的启动配置
json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch js",
"program": "${workspaceFolder}/hello.js",
"request": "launch",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
}
]
}
Run
点击"Launch js"运行hello.js

输出结果:

OK,基于VS Code的js运行环境就搭建完成啦!此处是直接执行完了,若要打断点,进行debug要怎么操作呢?
Debug

运行后,程序在第1行的断点处暂停了。

相较于纯命令行方式的运行,借用vscode可以非常方便的看到一些内置信息。当我们执行"step over"后,可以观察变量v1
的值发生了变化,同时"hello,javascript"也在终端打印了出来。

简单的运行环境搭建好了,可能自然的会产生一个疑问,如果是更复杂的工程项目,要怎样运行、调试呢?后面有空再结合具体的实例写一些博客。