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

本文介绍了如何在VSCode中利用nodemon模块设置项目,以实现TypeScript编译后的Node.js服务自动重启。通过生成.launch.json和.tasks.json文件,开发者可以便捷地实现代码修改后服务的自动编译和启动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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启动以下你的服务看一下吧是不是已经可以自动编译和重启服务了呢?

效果如下:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值