gpt4 book ai didi

node.js - 无法在 VS Code 中设置断点调试 Node Typescript

转载 作者:搜寻专家 更新时间:2023-10-30 20:40:38 25 4
gpt4 key购买 nike

我看到其他问题也有同样的问题,但我已经尝试了所有其他解决方案,但没有任何效果。

我有一个 typescript Node 应用程序,我正尝试在 VSCode 中进行调试。

我的launch.json是

 "version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 5858,
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/build/**/*.js"]
}
]

这非常适合我的应用程序。我可以暂停和恢复,一切正常,但我无法进入代码或设置断点。

我正在通过 gulp nodemon 运行我的应用

nodemon({
script: 'build/server.js',
watch: 'src',
ext: 'ts',
tasks: ['clean', 'compile'],
exec: 'node --debug'
});

控制台输出

Debugger listening on [::]:5858

现在当我尝试设置一个断点时它说

Unverified breakpoint, Breakpoint ignored because generated code not found (source map problem?).

更新;

我也尝试过按照其他帖子的建议使用 webRoot 项目,键入验证提示 Property webRoot is not allowed.,我尝试了无论如何都无济于事.

我正在运行 Node v6.11.5 和 VS Code v1.23.0

我在一篇帖子中看到人们呼吁运行 .scripts 标记以获取更多信息帮助解决但是当我通过在调试控制台中键入 .scripts 时它说 invalid expression : 意外标记。

我的tsconfig.json是

"compilerOptions": {
"outDir": "build",
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"typeRoots": ["node_modules/@types"]
},

但是;我的构建文件夹中没有 .js.map 文件。我正在通过 gulp-typescript 运行构建,如下所示

gulp.task('compile', () => {
tsProject = ts.createProject('tsconfig.json');
let tsResult = tsProject.src().pipe(ts());

return merge([
tsResult.dts.pipe(gulp.dest('build/definitions')),
tsResult.js.pipe(gulp.dest('build'))
]);
});

根据建议,我还添加了以下 gulp 任务

gulp.task('jsMaps', function() {
gulp.src('build/**/*.js')
.pipe(sourcemaps.init())
.pipe(sourcemaps.write())
.pipe(gulp.dest('build'));
});

并确认我的构建 .js 文件具有内联写入的源映射,看起来像 //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIiwic291cmNlc......,但我在尝试设置断点时仍然遇到同样的错误。

最佳答案

为了调试 typescript ,您需要生成源映射文件。确保以下内容在您的 tsconfig.json 中,您应该会在构建目录中看到生成的 .js.map 文件。

{
"compilerOptions": {
"sourceMap": true
}
}

使用 gulp-typescript:

gulp-typescript 建议 gulp-sourcemaps应该用于生成源映射。

这是我创建 .js.map 文件的 gulp 任务,该文件在断点处中断。找到in this gulp-typescript issue

var gulp = require('gulp');
var sourcemaps = require('gulp-sourcemaps');
var path = require('path');
var ts = require('gulp-typescript');

gulp.task('compile', () => {
tsProject = ts.createProject('tsconfig.json');
let tsResult = tsProject.src()
.pipe(sourcemaps.init())
.pipe(tsProject());

tsResult.js
.pipe(sourcemaps.write({
sourceRoot: function (file) {
var sourceFile = path.join(file.cwd, file.sourceMap.file);
return path.relative(path.dirname(sourceFile), file.cwd);
}
}))
.pipe(gulp.dest('build'));
});

关于node.js - 无法在 VS Code 中设置断点调试 Node Typescript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50203955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com