vscode 运行/调试 js/ts

js

js在安装node.js之后就可以直接在命令行中执行 node filename.jsnode执行js

ts

  1. 安装node.js

  2. npm i typescript tsc-node -D

  3. 根目录下新建tsconfig.json,用于ts配置

    {
      "compilerOptions": {
          "module": "commonjs",
          "target": "es6",
          "noImplicitAny": true,
          "outDir": "./dist",
          "sourceMap": true
        },
        "include": [
              "src/**/*",
              "**/*"
        ],
    }
    
  4. 根目录下新建.vscode文件夹,在文件加内新建launch.json

    {
      // 使用 IntelliSense 了解相关属性。 
      // 悬停以查看现有属性的描述。
      // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
      "version": "0.2.0",
      "configurations": [
        {
          "name": "启动程序",
          "type": "node",
          "request": "launch",
          "runtimeArgs": [
            "--nolazy",
            "-r",
            "ts-node/register"
          ],
          "sourceMaps": true,
          "cwd": "${workspaceRoot}",
          // "protocol": "inspector",
          "console": "integratedTerminal",
          // "internalConsoleOptions": "neverOpen",
          "skipFiles": [
            "<node_internals>/**"
          ],
          "program": "${file}",
          "outFiles": [
            "${workspaceFolder}/**/*.js"
          ]
        }
      ]
    }
    

调试步骤

  1. 点开需要调试的文件
  2. F5或点击菜单栏运行->启动调试 调试
  3. 调试页面 调试页面

参考网址

  1. 博客园open in new window
  2. 简书open in new window
Last Updated:
Contributors: haizhiguang