There is a great Node.js package for that: serialize-error
.
npm install serialize-error
It handles well even nested Error objects.
import {serializeError} from 'serialize-error';
JSON.stringify(serializeError(error));
Docs: https://www.npmjs.com/package/serialize-error