使用vscode 搭建 typescript 的nodejs 自动编译自动启动服务

1、在项目中安装nodemon 模块  

npm install nodemon --save -dev

2、在package.json中添加以下画红框的脚本


{
  "name": "server",
  "version": "0.0.0",
  "scripts": {
    "postinstall": "tsc -p .",
    "watch": "tsc -w -p .",
    "debug": "nodemon --watch ./build --inspect=0.0.0.0:5858 --nolazy ./build/index.js",
    "docker-debug": "docker-compose up",
    "start": "node ./build/index.js"
  },
  "devDependencies": {
    "@types/node": "^6.0.50",
    "typescript": "^2.3.2",
    "nodemon": "^1.11.0"
  },
  "main": "index.js"
}


3、生成.vscode 文件夹下的 launch.json   和 tasks.json 文件 


其中launch.json 如下:



{
    // Use IntelliSense to learn about possible Node.js debug attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "本地nodemon启动",
            "protocol": "auto",
            "preLaunchTask": "tsc-watch",
            "runtimeExecutable": "npm",
            "runtimeArgs": [
                "run",
                "debug"
            ],
            "env": {
                "NODE_ENV": "dev"
            },
            "restart": true,
            "port": 5858,
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen",
            "outFiles": [
                "${workspaceRoot}/build/**/*.js"
            ]
        }
    ]
}
tasks.json 如下:


{
    "version": "0.1.0",
    "tasks": [
        {
            "taskName": "tsc-watch",
            "command": "npm",
            "isShellCommand": true,
            "args": [
                "run",
                "watch"
            ],
            "isBackground": true,
            "isBuildCommand": true,
            "problemMatcher": "$tsc-watch",
            "showOutput": "always"
        }
    ]
}

使用f5启动以下你的服务看一下吧是不是已经可以自动编译和重启服务了呢?

效果如下:




你可能感兴趣的:(node.js,vscode)