Bru*_*tus 4 debugging node.js typescript visual-studio-code
我目前正在尝试从Visual Studio Code调试用TypeScript编写的Node JS应用程序,但我遇到了一些问题.我的情况类似于这个问题所描述的情况
|-- .settings
|----- launch.json
|-- bin
|----- app.js
|----- app.js.map
|--src
|----- app.ts
|-- tsconfig.json
Run Code Online (Sandbox Code Playgroud)
然后我有tsconfig.json文件:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"outDir": "bin",
"rootDir": "src",
"sourceMap": true
}
}
Run Code Online (Sandbox Code Playgroud)
该应用程序:
console.log("Hello World!");
Run Code Online (Sandbox Code Playgroud)
该launch.json:
{
"version": "0.1.0",
"configurations": [
{
"name": "Launch type",
"type": "node",
"program": "src/app.ts",
"stopOnEntry": false,
"sourceMaps": true,
"outDir": "bin"
}
]
}
Run Code Online (Sandbox Code Playgroud)
然后我从命令行手动编译项目
tsc
Run Code Online (Sandbox Code Playgroud)
所以我在bin目录中得到两个文件.我在app.ts上设置了一个断点,最后用F5运行解决方案,应用程序启动并停在右边但是在JS文件而不是TS一个:为什么???
我做错了什么或试图实现不可能的事情?
非常感谢您的帮助!:)
编辑
我刚刚在GitHub上分享了我的项目,以便让事情变得更轻松!看看你能不能!:)
绝对可能.
最可能的原因是node.js无法使用生成的map.js文件找到相应的ts文件.您可以尝试在tsconfig.json中指定"sourceRoot"以指向项目的根目录:
sourceRoot: "/Users/SomeUser/projects/test"
Run Code Online (Sandbox Code Playgroud)
就个人而言,我更喜欢使用gulp用于此目的,在我的情况下,它看起来像这样(注意 - 我不使用node.js全局变量'__dirname'来核心sourceRoot路径):
var ts = require('gulp-typescript');
gulp.task('build.js.dev', function()
{
var tsProject = ts.createProject('tsconfig.json');
var tsResult = tsProject.src()
.pipe(sourcemaps.init())
.pipe(ts(tsProject));
return merge([
//Write definitions
//tsResult.dts.pipe(gulp.dest("bin")),
//Write compiled js
tsResult.js.pipe(sourcemaps.write("./", { sourceRoot: __dirname })).pipe(gulp.dest("bin"))]);
});
Run Code Online (Sandbox Code Playgroud)
之后检查生成的map.js文件.它应该在开头包含类似这样的行:
"sources":["src/app.ts"]
Run Code Online (Sandbox Code Playgroud)
最后:
"sourceRoot":"/Users/SomeUser/projects/test"
Run Code Online (Sandbox Code Playgroud)
组合在一起时,它们必须指向app.ts文件的有效位置.如果不是 - 相应地调整sourceRoot.
[编辑]
以下是与您的项目相同的项目部分(没有gulp) - 我可以在我的机器上进行调试.
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"
}
Run Code Online (Sandbox Code Playgroud)
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/**"
]
}
Run Code Online (Sandbox Code Playgroud)
并在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"
}
Run Code Online (Sandbox Code Playgroud)
[编辑]
我已经对您的git存储库进行了以下次要更新,以便能够在本地进行调试.
在根文件夹中添加package.json,并在那里指定tsc作为依赖项(我更喜欢本地安装):
{
"name": "123",
"namelower": "123",
"version": "0.0.1",
"private": true,
"dependencies": {
},
"devDependencies": {
"typescript": "latest"
}
}
Run Code Online (Sandbox Code Playgroud)
然后转到你的git"stackoverflow"根文件夹并在命令提示符下运行:
npm install
Run Code Online (Sandbox Code Playgroud)
将tasks.json"命令"行更改为:
"command": "${workspaceRoot}/node_modules/typescript/bin/tsc",
Run Code Online (Sandbox Code Playgroud)
在完成这些步骤并构建项目后,我能够在app.ts中放置一个断点,并在运行时将VSCode停在它上面(F5)
[UPDATE]
与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"
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
归档时间: |
|
查看次数: |
3909 次 |
最近记录: |