我正在开发一个Nest.js服务器,希望能够在控制台(例如console.log)中打印有用的堆栈跟踪。默认情况下,它返回对编译源(.js)中行号的引用。这对调试没有帮助,因为它缺少了对原始源文件(.ts)中行号的引用。
这是我的tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"_baseUrl": "./",
"incremental": true
},
"exclude": ["node_modules", "dist"]
}
.map文件也是在dist文件夹中生成的,不过在检查控制台中的堆栈跟踪时似乎没有用。
发布于 2019-11-22 13:28:33
为了便于看到:添加源-地图支持 NPM包允许在堆栈跟踪中跟踪类型记录文件。
它可以用node -r source-map-support/register fileToRun.js
添加到命令行中,也可以用
import * as sourceMapSupport from 'source-map-support';
sourceMapSupport.install();
或
import { install } from 'source-map-support';
install();
或使用ES6模块
import 'source-map-support/register';
发布于 2022-10-12 10:26:50
如果我还添加了一个
webpack.config.js
文件到NestJS项目的根目录中,包含以下内容:
// webpack.config.js
module.exports = function(options) {
return {
...options,
devtool: 'inline-source-map',
}
}
有了这个文件,当您将NestJS源转到main.js上时,可以根据您的需要配置Webpack。
在main.ts中,我添加了如下行,如上面的答案所述:
import * as sourceMapSupport from 'source-map-support';
sourceMapSupport.install();
瞧,它的工作和准确的打字文件加上行号都显示在控制台堆栈跟踪中。
发布于 2022-11-14 11:45:41
根据内斯奇医生,有两种配置热重加载的方法。
在我的例子中,我使用了利用巢cli的方法。
步骤:
yarn add source-map-support @types/source-map-support
import "source-map-support/register"
添加到main.ts
中devtool: 'inline-source-map'
添加到webpack-hmr.config.js
中然后瞧!它如预期的那样工作。
以下是完成步骤3后的webpack-hmr.config.js
:(与文档中相同,但添加了一行)
const nodeExternals = require('webpack-node-externals');
const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');
module.exports = function (options, webpack) {
return {
...options,
entry: ['webpack/hot/poll?100', options.entry],
externals: [
nodeExternals({
allowlist: ['webpack/hot/poll?100'],
}),
],
plugins: [
...options.plugins,
new webpack.HotModuleReplacementPlugin(),
new webpack.WatchIgnorePlugin({
paths: [/\.js$/, /\.d\.ts$/],
}),
new RunScriptWebpackPlugin({
name: options.output.filename,
autoRestart: false,
}),
],
devtool: 'inline-source-map',
};
};
如果您正在配置热模块重新加载而没有嵌套cli,那么在步骤3中将该行添加到webpack.config.js
而不是webpack-hmr.config.js
中。
https://stackoverflow.com/questions/59000552
复制相似问题