In V8 (Chrome, Node.js, Deno, etc.),
error.stack
includes
error.message.
However, if error.stack is modified, error.message is not updated
accordingly. This library fixes it.
In other JavaScript engines, this library just sets error.stack.
Example
Without set-error-stack:
const error = new Error('one')console.log(error.stack) // 'Error: one ...'console.log(error.message) // 'one'error.stack = error.stack.replace('one', 'two')console.log(error.stack) // 'Error: two ...'console.log(error.message) // 'one'
With set-error-stack:
import setErrorStack from 'set-error-stack'const error = new Error('one')console.log(error.stack) // 'Error: one ...'console.log(error.message) // 'one'setErrorStack(error, error.stack.replace('one', 'two'))console.log(error.stack) // 'Error: two ...'console.log(error.message) // 'two'
Install
npm install set-error-stack
This package works in both Node.js >=18.18.0 and
browsers.