nwjs/nw.js · error · TypeError

'click' must be a valid Function

Error message

'click' must be a valid Function

What it means

Thrown by the nw.MenuItem constructor when 'click' is present in options but is not a function. The click handler is invoked on item activation, so it must be callable.

Source

Thrown at src/resources/api_nw_menuitem.js:67

      option.iconIsTemplate = Boolean(option.iconIsTemplate);
    else
      option.iconIsTemplate = true;

    if (option.hasOwnProperty('tooltip'))
      option.tooltip = String(option.tooltip);

    if (option.hasOwnProperty('enabled'))
      option.enabled = Boolean(option.enabled);

    if (option.hasOwnProperty('submenu')) {
      // Transfer only object id
      privates(this).submenu = option.submenu;
      option.submenu = option.submenu.id;
    }

    if (option.hasOwnProperty('click')) {
      if (typeof option.click != 'function')
        throw new TypeError("'click' must be a valid Function");
      else
        this.click = option.click;
    }
  } else if (option.type == 'separator') {
    option = {
      type: 'separator'
    };
  }

  var id = option.id || nw.Obj.allocateId();
  this.id = id;
  privates(this).option = option;

  menuItems.objs[id] = this;
  // All properties must be set after initialization.
  if (!option.hasOwnProperty('icon'))
    option.shadowIcon = '';
  if (!option.hasOwnProperty('tooltip'))

View on GitHub (pinned to e15da848e9)

Solutions

  1. Pass a function reference: `new nw.MenuItem({ label: 'x', click: doClick })`.
  2. Bind if needed: `click: obj.doClick.bind(obj)`.
  3. Omit 'click' and use item.on('click', fn) after construction.

Example fix

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

Strategy: type-guard

Validate before calling

if (option.hasOwnProperty('click') && typeof option.click !== 'function') delete option.click;

Type guard

function isClickFn(v) { return typeof v === 'function'; }

Try / catch

try { new nw.MenuItem(opt); } catch (e) { if (/click/.test(e.message)) { delete opt.click; new nw.MenuItem(opt); } else throw e; }

Prevention

When it happens

Trigger: Calling `new nw.MenuItem({ label: 'x', click: 'doClick' })` or `click: undefined` after a failed lookup.

Common situations: Passing a handler name string from config; the method reference did not bind correctly.

Related errors


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