nwjs/nw.js · error · TypeError

'menu' property requries a valid Menu

Error message

'menu' property requries a valid Menu

What it means

Thrown by the nw.Tray 'menu' setter when the assigned value's constructor name is not 'Menu'. This is the runtime-mutation equivalent of the constructor guard: tray.menu can only be reassigned to a real nw.Menu instance.

Source

Thrown at src/api/tray/tray.js:133

Tray.prototype.__defineSetter__('iconsAreTemplates', function(val) {
  this.handleSetter('iconsAreTemplates', 'SetIconsAreTemplates', Boolean, val);
});

Tray.prototype.__defineGetter__('tooltip', function() {
  return this.handleGetter('tooltip');
});

Tray.prototype.__defineSetter__('tooltip', function(val) {
  this.handleSetter('tooltip', 'SetTooltip', String, val);
});

Tray.prototype.__defineGetter__('menu', function() {
  return v8_util.getHiddenValue(this, 'menu');
});

Tray.prototype.__defineSetter__('menu', function(val) {
  if (v8_util.getConstructorName(val) != 'Menu')
    throw new TypeError("'menu' property requries a valid Menu");

  v8_util.setHiddenValue(this, 'menu', val);
  nw.callObjectMethod(this, 'SetMenu', [ val.id ]);
});

Tray.prototype.remove = function() {
  nw.callObjectMethod(this, 'Remove', []);
}

Tray.prototype.handleEvent = function(ev) {
 if (ev == 'click') {
   // Emit click handler
   if (typeof this.click == 'function'){
     this.click();
   }
 }
 // Emit generate event handler
 exports.Base.prototype.handleEvent.apply(this, arguments);

View on GitHub (pinned to e15da848e9)

Solutions

  1. Assign a nw.Menu instance: `tray.menu = new nw.Menu()`.
  2. To clear, assign a new empty Menu rather than null or undefined.

Example fix

// before
tray.menu = { items: [] };
// after
tray.menu = new nw.Menu();
Defensive patterns

Strategy: type-guard

Validate before calling

if (val && !(val instanceof nw.Menu)) throw new Error('tray.menu requires a nw.Menu');

Type guard

function isMenuInstance(v) { return v && v.constructor && v.constructor.name === 'Menu'; }

Try / catch

try { tray.menu = val; } catch (e) { if (/Menu/.test(e.message)) tray.menu = new nw.Menu(); else throw e; }

Prevention

When it happens

Trigger: Running `tray.menu = null`, `tray.menu = { items: [] }`, or `tray.menu = somePlainObject` after construction.

Common situations: Clearing the menu by assigning null instead of removing it; replacing the menu with a freshly parsed JSON object.

Related errors


AI-assisted analysis of nwjs/nw.js@e15da848e9 (2026-08-13). Data as JSON: /api/errors/f07e032726e906df. Report an issue: GitHub.