nwjs/nw.js · error · TypeError

Invalid option.

Error message

Invalid option.

What it means

Thrown by the nw.MenuItem constructor when the option argument is not an object (typeof check). MenuItem reads type, label, icon, etc. from the option object, so a primitive or undefined is rejected up front.

Source

Thrown at src/resources/api_nw_menuitem.js:24

var menuItems = { objs : {}, clickEvent: {} };
menuItems.clickEvent = bindingUtil.createCustomEvent("NWObjectclick", false, false);
menuItems.clickEvent.addListener(function(id) {
  var obj = menuItems.objs[id];
  if (!obj)
    return;
  try{obj.click && obj.click()}catch(e){console.error(e)}
  try{obj.emit('click')}catch(e){console.error(e)}
});

function MenuItem(option) {
  if (!(this instanceof MenuItem)) {
    return new MenuItem(option);
  }
  EventEmitter.apply(this);

  if (typeof option != 'object')
    throw new TypeError('Invalid option.');

  if (!option.hasOwnProperty('type'))
    option.type = 'normal';

  if (option.type != 'normal' &&
      option.type != 'checkbox' &&
      option.type != 'separator')
    throw new TypeError('Invalid MenuItem type: ' + option.type);

  if (option.type == 'normal' || option.type == 'checkbox') {
    if (option.type == 'checkbox')
      option.checked = Boolean(option.checked);

    if (!option.hasOwnProperty('label'))
      throw new TypeError('A normal MenuItem must have a label');
    else
      option.label = String(option.label);

View on GitHub (pinned to e15da848e9)

Solutions

  1. Wrap in an object: `new nw.MenuItem({ label: 'Quit' })`.
  2. Default the argument: `new nw.MenuItem(opt || { type: 'separator' })`.

Example fix

// before
new nw.MenuItem('Quit');
// after
new nw.MenuItem({ label: 'Quit' });
Defensive patterns

Strategy: validation

Validate before calling

if (typeof option !== 'object' || option === null) option = {};

Type guard

function isMenuItemOption(o) { return o && typeof o === 'object'; }

Try / catch

try { new nw.MenuItem(opt); } catch (e) { if (/Invalid option/.test(e.message)) new nw.MenuItem({ label: '' }); else throw e; }

Prevention

When it happens

Trigger: Calling `new nw.MenuItem()`, `new nw.MenuItem(null)`, `new nw.MenuItem('Quit')`, or `new nw.MenuItem(0)`.

Common situations: Passing a bare label string as in some other menu APIs; forgetting to wrap configuration in an object.

Related errors


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