Node.js Visual Studio代码-通过TypeScript调试节点JS
我目前正试图调试一个用VisualStudio代码中的TypeScript编写的NodeJS应用程序,但我遇到了一些问题。 我的情况与上面描述的类似 然后是tsconfig.json文件:Node.js Visual Studio代码-通过TypeScript调试节点JS,node.js,debugging,typescript,visual-studio-code,Node.js,Debugging,Typescript,Visual Studio Code,我目前正试图调试一个用VisualStudio代码中的TypeScript编写的NodeJS应用程序,但我遇到了一些问题。 我的情况与上面描述的类似 然后是tsconfig.json文件: { "compilerOptions": { "module": "commonjs", "target": "es5", "outDir": "bin", "rootDir": "src", "sourceMap": t
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"outDir": "bin",
"rootDir": "src",
"sourceMap": true
}
}
应用程序ts:
console.log("Hello World!");
{
"version": "0.1.0",
"configurations": [
{
"name": "Launch type",
"type": "node",
"program": "src/app.ts",
"stopOnEntry": false,
"sourceMaps": true,
"outDir": "bin"
}
]
}
launch.json:
console.log("Hello World!");
{
"version": "0.1.0",
"configurations": [
{
"name": "Launch type",
"type": "node",
"program": "src/app.ts",
"stopOnEntry": false,
"sourceMaps": true,
"outDir": "bin"
}
]
}
然后,我使用
tsc
因此,我在bin目录中得到了两个文件。我在app.ts上设置了一个断点,最后用F5运行解决方案,应用程序在右边的行启动和停止,但在JS文件上,而不是ts一个:为什么
我是做错了什么,还是试图实现不可能的事情
非常感谢您的帮助!:)
编辑
我刚刚在上分享了我的项目,以便让事情变得更简单!如果可以,请看一看!:) 这是绝对可能的 最可能的原因是node.js无法使用生成的map.js文件定位相应的ts文件。 您可以尝试在tsconfig.json中指定“sourceRoot”以指向项目的根:
sourceRoot: "/Users/SomeUser/projects/test"
就我个人而言,我更喜欢使用gulp实现这一目的,在我的例子中,它看起来是这样的(注意-我在这里不使用node.js全局变量'\uu dirname'):
然后检查生成的map.js文件。它应该包含类似于开头几行的内容:
"sources":["src/app.ts"]
最后:
"sourceRoot":"/Users/SomeUser/projects/test"
组合在一起时,它们必须指向app.ts文件的有效位置。如果没有-相应地调整sourceRoot
[编辑]
下面是项目中与您相同的部分(无需吞咽)-我可以在我的机器上调试
launch.json:
{
// Name of configuration; appears in the launch configuration drop down menu.
"name": "Launch Server",
// Type of configuration.
"type": "node",
// Workspace relative or absolute path to the program.
"program": "${workspaceRoot}/src/app.ts",
// Automatically stop program after launch.
"stopOnEntry": false,
// Command line arguments passed to the program.
"args": [],
// Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
"cwd": "${workspaceRoot}",
// Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
"runtimeExecutable": null,
// Optional arguments passed to the runtime executable.
"runtimeArgs": ["--nolazy"],
// Environment variables passed to the program.
"env": {
"NODE_ENV": "development"
},
// Use JavaScript source maps (if they exist).
"sourceMaps": true,
// If JavaScript source maps are enabled, the generated code is expected in this directory.
"outDir": "${workspaceRoot}/bin",
"request": "launch"
}
tsconfig.json:
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"module": "commonjs",
"target": "es6",
"sourceMap": true,
"outDir": "bin",
"declaration": true,
"noImplicitAny": true
},
"exclude": [
"node_modules",
"bin",
".vscode",
"typings/browser.d.ts",
"typings/browser/**"
]
}
并在tasks.json中生成任务:
{
"version": "0.1.0",
// The command is tsc. Assumes that tsc has been installed locally using npm install typescript
"command": "${workspaceRoot}/node_modules/typescript/bin/tsc",
// The command is a shell script
"isShellCommand": true,
// Show the output window only if unrecognized errors occur.
"showOutput": "silent",
// args is the HelloWorld program to compile.
"args": [],
// use the standard tsc problem matcher to find compile problems
// in the output.
"problemMatcher": "$tsc"
}
[编辑]
我已经对您的git存储库进行了以下小更新,以便能够在本地对其进行调试
在根文件夹中添加package.json,并将那里的tsc指定为依赖项(我更喜欢本地安装):
然后转到git“stackoverflow”根文件夹并在命令提示符下运行:
npm install
将tasks.json“命令”行更改为:
在完成这些步骤并构建项目之后,我能够在app.ts中放置一个断点,并在运行时在其上停止VSCode(F5)
[更新]
与windows兼容的tasks.json版本:
{
"version": "0.1.0",
"command": "tsc",
"showOutput": "always",
"windows": {
"command": "node.exe"
},
"args": ["${workspaceRoot}\\node_modules\\typescript\\bin\\tsc.js"],
"problemMatcher": "$tsc"
}
希望这有帮助。这就是我调试typescript(express)项目的方式。使用不必手动编译的ts节点。使用这个配置,我直接在我的typescript文件中调试。希望这对别人有帮助
{
"version": "0.2.0",
"configurations": [
{
"name": "Current TS File",
"type": "node",
"request": "launch",
"args": ["${workspaceRoot}/src/app.ts"],
"runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
"sourceMaps": true,
"cwd": "${workspaceRoot}",
"protocol": "inspector"
}
]
}
在试图找出如何使用aws Lambda调试TS+NodeJ时遇到了相同的问题。在watchTS文件更改方面,该包似乎比
nodemon
更快
npm安装ts节点开发人员--保存开发人员
最后在launch.json
中添加以下配置:
{
“版本”:“1.0.0”,
“配置”:[
{
“控制台”:“集成终端”,
“internalConsoleOptions”:“neverOpen”,
“名称”:“本地服务器”,
“重启”:正确,
“请求”:“启动”,
“runtimeExecutable”:“${workspaceRoot}/node_modules/.bin/ts node dev”,
“滑雪板”:[
"/**"
],
“类型”:“节点”,
“runtimeArgs”:[
“重生”
],
“args”:[
“${workspaceFolder}/src/script/local.server.ts”
]
}
]
}
按F5
应使用本机http
或express/koa/hapi…
等启动本地服务器。现在您可以在代码中设置断点。此外,ts节点开发人员将在每次保存时监视ts文件的更改和重新加载服务器
如果您碰巧使用aws lambda
https://github.com/vcfvct/ts-lambda-local-dev
我尝试了你的建议(没有吞咽),但它不起作用,即使在地图文件中的路径是确定的。您是否使用最新的代码版本(0.10.8)?是-我使用。我在launch.json中包含了我的配置部分,它非常适合我。实际上,我的版本是0.10.9。很久以前我就开始使用VSCode/node.js了,所以我认为它的版本不相关。我已经添加了与您相同的测试项目的所有“部分”,没有使用gulp。非常感谢!你解决了我的问题!我按照中的说明进行操作,在编译时出现以下错误:“c:\Users\brutus\Desktop\stackoverflow master/node\u modules/typescript/bin/tsc”未被识别为内部或外部命令,然后我将该命令还原为“tsc”,它工作正常,而且调试器在正确的源文件中停止了:)我很好奇在GitHub上检查原始版本,它也在工作,没有package.json文件!我不知道为什么,也许我用的是过时的打字稿版本。非常感谢,你太棒了!:)
{
"version": "0.2.0",
"configurations": [
{
"name": "Current TS File",
"type": "node",
"request": "launch",
"args": ["${workspaceRoot}/src/app.ts"],
"runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
"sourceMaps": true,
"cwd": "${workspaceRoot}",
"protocol": "inspector"
}
]
}
https://github.com/vcfvct/ts-lambda-local-dev