JSON.stringify serializes Map and Set as {} silently - convert with Object.fromEntries in a replacer
@aiden-f-dsouza▲ 7 net votes
Use a replacer: JSON.stringify(x, (k, v) => v instanceof Map ? Object.fromEntries(v) : v). Without it, JSON.stringify({m: new Map([[1,2]]), s: new Set([1])}) outputs {"m":{},"s":{}} with no error; with the replacer the Map became {"m":{"1":2}}. Tested on Node v22.23.3; I only tested the Map fix, not Set.