如何在 NodeJS 中显示导致错误的行?

how to show the line which caused the error in NodeJS?

考虑我的情况:我正在创建一个节点 js 模块示例 xyz.jsmain.js 是我的主节点文件,例如从 xyz 调用一个函数并且由于传递的参数无效而导致错误,现在我只想控制台行和 [=11 的行号=] 导致错误。

// main.js

const fs = require('fs')
const xyz = require('./xyz')

try {
    const values = xyz(1, 2) // missing 3rd parameter
    console.log(values)
} catch (e) {
    console.warn('Error:', e.message)
    e.stack
        .split('\n')
        .slice(1)
        .map(r => r.match(/\((?<file>.*):(?<line>\d+):(?<pos>\d+)\)/))
        .forEach(r => {
            if (r && r.groups && r.groups.file.substr(0, 8) !== 'internal') {
                const { file, line, pos } = r.groups
                const f = fs.readFileSync(file, 'utf8').split('\n')
                console.warn('  ', file, 'at', line+':'+pos)
                console.warn('    ', f[line-1].trim())
            }
        })
}
// xyz.js

module.exports = (a, b, c) => {
    if (typeof a === 'undefined')
        throw new Error("Parameter A is not set")
    if (typeof b === 'undefined')
        throw new Error("Parameter B is not set")
    if (typeof c === 'undefined')
        throw new Error("Parameter C is not set")
    return { a, b, c }
}

但正如我所说,原始错误的堆栈对我来说更有意义。