nwjs/nw.js · error · TypeError

Invalid menu type: {option.type}

Error message

Invalid menu type: {option.type}

What it means

Thrown by the nw.Menu constructor when option.type is present but is neither 'contextmenu' nor 'menubar'. These are the only two menu kinds nw supports (a popup context menu or an application menu bar).

Source

Thrown at src/resources/api_nw_menu.js:13

var forEach = require('utils').forEach;
var nwNative = requireNative('nw_natives');
var messagingNatives = requireNative('messaging_natives');

function Menu (option) {
  if (!(this instanceof Menu)) {
    return new Menu(option);
  }

  if (typeof option != 'object' || !option)
    option = { type: 'contextmenu' };
  if (option.type != 'contextmenu' && option.type != 'menubar')
    throw new TypeError('Invalid menu type: ' + option.type);

  var id = nw.Obj.allocateId();
  option.generatedId = id;

  this.id = id;
  this.type = option.type;
  privates(this).items = [];
  privates(this).option = option;

  var items = privates(this).items;
  nw.Obj.create(id, 'Menu', option);
  messagingNatives.BindToGC(this, function() { items.forEach(function(element) { element._destroy(); }); nw.Obj.destroy(id); });
};

Menu.prototype.__defineGetter__('items', function() {
  return privates(this).items;
});

View on GitHub (pinned to e15da848e9)

Solutions

  1. Use 'contextmenu' or 'menubar' exactly (lowercase).
  2. Omit type entirely to default to 'contextmenu'.

Example fix

// before
new nw.Menu({ type: 'Menubar' });
// after
new nw.Menu({ type: 'menubar' });
Defensive patterns

Strategy: validation

Validate before calling

const allowed = ['contextmenu', 'menubar'];
if (option && option.type && allowed.indexOf(option.type) === -1) option.type = 'contextmenu';

Type guard

function isMenuType(v) { return v === 'contextmenu' || v === 'menubar'; }

Try / catch

try { new nw.Menu(opt); } catch (e) { if (/Invalid menu type/.test(e.message)) new nw.Menu(); else throw e; }

Prevention

When it happens

Trigger: Calling `new nw.Menu({ type: 'toolbar' })`, `new nw.Menu({ type: 'dropdown' })`, or `new nw.Menu({ type: 'Menubar' })` (case mismatch).

Common situations: Typo in the type string; using a type name from a different framework (Electron uses 'submenu'); case sensitivity.

Related errors


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