【学习笔记】在VSCode上配置typescript + nodejs 开发环境
来源:互联网 发布:js 跳转url target 编辑:程序博客网 时间:2024/06/02 07:18
确认需要的都安装好了
TypeScript中文官网:https://www.tslang.cn/
配置编译任务
在VSCode中快捷键 Ctrl + Shift + B
用来编译,对应的编译配置文件为.vscode/task.json
第一次按会弹出 No task runner configured
。点击configure task runner
开始配置。 选择TypeScript with Watch Mode
会在.vscode
目录下自动创建task.json
如下
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "0.1.0", "command": "tsc", "isShellCommand": true, "args": ["-w", "-p", "."], "showOutput": "silent", "isWatching": true, "problemMatcher": "$tsc-watch"}
有了这个task.json
后每次Ctrl + Shift + B
即可调用tsc
命令编译typescript
。
task配置的官方文档
https://code.visualstudio.com/docs/editor/tasks
tsc
命令会查找当前目录的tsconfig.json
配置来编译typescript
创建tsconfig.json
node
还不支持es6
的import
,所以还是先编译到es5
{ "compilerOptions": { "target": "es5", "outDir": "build", "sourceMap": true }}
tsconfig.json的文档
https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
Debug配置
Ctrl + Shift + D
点击配置图标,选择nodejs
会在.vscode目录下自动创建launch.json如下
{ "version": "0.2.0", "configurations": [ { "name": "Launch", "type": "node", "request": "launch", "program": "${workspaceRoot}/app.js", "stopOnEntry": false, "args": [], "cwd": "${workspaceRoot}", "preLaunchTask": null, "runtimeExecutable": null, "runtimeArgs": [ "--nolazy" ], "env": { "NODE_ENV": "development" }, "externalConsole": false, "sourceMaps": false, "outDir": null }, { "name": "Attach", "type": "node", "request": "attach", "port": 5858, "address": "localhost", "restart": false, "sourceMaps": false, "outDir": null, "localRoot": "${workspaceRoot}", "remoteRoot": null }, { "name": "Attach to Process", "type": "node", "request": "attach", "processId": "${command.PickProcess}", "port": 5858, "sourceMaps": false, "outDir": null } ]}
修改上边的Launch部分几项,使VSCode认得typescript
和sourceMap
"program": "${workspaceRoot}/app.ts","sourceMaps": true,"outDir": "${workspaceRoot}/build"
这样就可以在typescript
上打断点调试了
debug的文档
https://code.visualstudio.com/docs/editor/debugging
语法提示
命令
typings install dt~node --global --save
会安装node语法提示,并创建typings.json
保存.d.ts
的版本
{ "globalDependencies": { "node": "registry:dt/node#6.0.0+20160807145350" }}
typings 文档
https://github.com/typings/typings
现在全部配置完毕,可以开始用typescript
写node了。
创建app.ts
import { createServer, Server, IncomingMessage, ServerResponse } from 'http';// 明确类型麻烦些,却会获得非常详细的语法提示const server: Server = createServer((req: IncomingMessage, res: ServerResponse) => { res.statusCode = 200; res.setHeader("Content-Type", "text/plain"); res.end("Hello World\n");})const hostname: string = "127.0.0.1";const port: number = 3000;server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`);})
也可以把所有函数import到http名下
import * as http from 'http';const server: http.Server = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => { res.statusCode = 200; res.setHeader("Content-Type", "text/plain"); res.end("Hello World\n");})const hostname: string = "127.0.0.1";const port: number = 3000;server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`);})
Ctrl + Shift + B
编译后就可以 Ctrl + Shift + D
进入debug模式,点击launch
运行程序了。
- 【学习笔记】在VSCode上配置typescript + nodejs 开发环境
- vscode开发nodejs配置备忘录(typescript)
- 如何在vscode 中配置:TypeScript开发node环境
- vscode开发Typescript笔记
- VScode搭建TypeScript开发环境
- 如何在vscode配置php开发环境
- 使用VsCode + Node.js搭建TypeScript开发环境
- NodeJs之TypeScript开发环境--VS Code
- TypeScript学习-TypeScript环境配置
- Node学习笔记——eclipse配置nodejs开发环境
- vscode 配置go开发环境
- (转)Rust: 在vscode配置 Rust 开发环境
- 使用VSCode开发Nodejs
- 如何在VSCode中配置python环境
- vscode中配置golang开发环境
- vscode 配置 go 语言开发环境
- ubuntu 16.04 VSCode 配置C++开发环境
- Unity3D在mac上使用VSCODE开发
- JS中正则表达式规范
- Coins-(DP)
- Javascript中call和apply的区别与详解
- ionic上传身份证正反面照片、上传图片、FileReader使用
- 题目1172:哈夫曼树
- 【学习笔记】在VSCode上配置typescript + nodejs 开发环境
- idea 启动tomcat报错 Application Server was not connected before run configuration stop, reason: Unable t
- HDU-4729 Number
- TCP建立连接时三次握手,断开时为何4次握手
- java nio
- E
- H5基础知识第十四课时(用JQuery确定鼠标位置)
- web-app 消息推送功能(浏览器为chrome59,firefox54)
- 微信新增临时素材-图片