1、在项目中安装nodemon 模块
npm install nodemon --save -dev
2、在package.json中添加以下脚本
(注意配置文件里的 src 路径问题 )
{ "name": "server", "version": "0.0.0", "scripts": { "postinstall": "tsc -p .", "watch": "tsc -w -p .", "debug": "nodemon --watch ./src --inspect=0.0.0.0:5858 --nolazy ./src/*.js", "docker-debug": "docker-compose up", "start": "node ./src/*.js" }, "devDependencies": { "@types/node": "^6.0.50", "typescript": "^2.3.2", "nodemon": "^1.11.0" }, "main": "*.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", "env": { "NODE_ENV": "dev" }, "restart": false, "console": "integratedTerminal", "internalConsoleOptions": "neverOpen", "outFiles": [ "${workspaceFolder}/src/js/test.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 或者 点击菜单 任务-运行任务 点击tsc-watch 即可!