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.Tray constructor when 'click' is present in options but is not a function. The click handler is invoked on left-click of the tray icon, so it must be callable.

Source

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

    option.icon = nw.getAbsolutePath(option.icon);
  }

  if (option.hasOwnProperty('alticon')) {
    option.shadowAlticon = String(option.alticon);
    option.alticon = nw.getAbsolutePath(option.alticon);
  }

  if (option.hasOwnProperty('iconsAreTemplates'))
    option.iconsAreTemplates = Boolean(option.iconsAreTemplates);
  else
    option.iconsAreTemplates = true;

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

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

  if (option.hasOwnProperty('menu')) {
    if (v8_util.getConstructorName(option.menu) != 'Menu')
      throw new TypeError("'menu' must be a valid Menu");

    // Transfer only object id
    v8_util.setHiddenValue(this, 'menu', option.menu);
    option.menu = option.menu.id;
  }

  v8_util.setHiddenValue(this, 'option', option);
  nw.allocateObject(this, option);

  // All properties must be set after initialization.

View on GitHub (pinned to e15da848e9)

Solutions

  1. Provide a function reference for 'click'.
  2. Omit 'click' if you do not need left-click handling and use the 'click' event via on() instead.

Example fix

// before
new nw.Tray({ icon: 'i.png', click: 'trayClicked' });
// after
new nw.Tray({ icon: 'i.png', click: trayClicked });
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Calling `new nw.Tray({ icon: 'i.png', click: 'onClick' })` or passing a reference that resolved to undefined.

Common situations: Storing the handler name as a string in config; passing an object wrapper instead of the bound method.

Related errors


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