Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

JSON.stringify on an Error gives an empty object

ErrorJSON.stringify(err) prints {}

Short answer

JSON.stringify only writes own enumerable properties, and an Error keeps its message as a non-enumerable one, so you get {}. Copy the fields you need into a plain object, or pass a replacer, or define toJSON on your error class. Use the cause option to keep the original error.

  • Applies to Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Compare what the error has with what JSON.stringify can see. If Object.keys is empty while err.message is set, you are in the first cause.

    const err = new Error('boom');
    console.log(Object.keys(err), err.message);
    console.log(JSON.stringify(err));
  2. 2Match what you see, then jump to its fix

    1. 1Object.keys(err) prints [] but err.message holds textmessage is not an enumerable own propertyFix
    2. 2Errors appear as {} in logs from many places, and you cannot touch each call siteYou need every Error in your app serialized the same wayFix
    3. 3The log shows only the wrapper's message, not why it failedThe original error is lost when you rethrowFix

What does the error look like?

  • JSON.stringify(err) prints {}
  • res.json({ error: err }) sends {}

Cause 1 of 3

message is not an enumerable own property

1Check

JSON.stringify visits only own enumerable string-keyed properties. The Error constructor creates message as a non-enumerable own property, so it is skipped.

console.log(Object.getOwnPropertyDescriptor(new Error('boom'), 'message'));

2Fix

Build a plain object from the fields you want to send. Do not send stack to clients.

const body = { name: err.name, message: err.message };
console.log(JSON.stringify(body));

3Verify

The output now contains the message.

node -e "const e=new Error('boom');console.log(JSON.stringify({name:e.name,message:e.message}))"

Confirmed by tc39.es

Did this fix it?

Cause 2 of 3

You need every Error in your app serialized the same way

1Check

A value with a toJSON method is serialized through that method, and a replacer function sees every key and value before it is written. Either hook can turn an Error into a plain object.

grep -rn "JSON.stringify" src/ | head

2Fix

Pass a replacer that converts Error values, or give your own error class a toJSON method.

const replacer = (key, value) =>
  value instanceof Error
    ? { name: value.name, message: value.message, cause: value.cause }
    : value;
JSON.stringify({ ok: false, error: err }, replacer);

class AppError extends Error {
  toJSON() {
    return { name: this.name, message: this.message };
  }
}

3Verify

A nested error now prints its fields.

console.log(JSON.stringify({ error: new AppError('boom') }));

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 3

The original error is lost when you rethrow

1Check

A cause is stored only when you pass it in the options argument, and a custom subclass must forward that argument to super().

class DbError extends Error {
  constructor(message, options) {
    super(message, options);
  }
}

2Fix

Wrap with the cause option, and include err.cause in what you log.

try {
  await db.query(sql);
} catch (e) {
  throw new DbError('query failed', { cause: e });
}

3Verify

The caught error exposes the original through cause.

console.log(caught.cause instanceof Error);

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Send clients a small object you build (a code and a message), never a raw Error.
  • Log errors with console.error(err) or a logger that understands Error, which prints the stack.
  • Pass { cause } when wrapping errors, and forward options to super() in custom classes.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.