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 Tray constructor when option.hasOwnProperty('click') is true but typeof option.click !== 'function'. The click callback is wired into the native tray click event; it must be callable. Validated eagerly at construction.

Source

Thrown at src/resources/api_nw_tray.js:56

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

  if (option.hasOwnProperty('alticon')) {
    option.shadowAlticon = String(option.alticon);
    option.alticon = nwNative.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 (option.menu.constructor.name != 'Menu')
      throw new TypeError("'menu' must be a valid Menu");

    // Transfer only object id
    privates(this).menu = option.menu;
    option.menu = option.menu.id;
  }
  
  var id = nw.Obj.allocateId();
  this.id = id;
  privates(this).option = option;

View on GitHub (pinned to e15da848e9)

Solutions

  1. Pass a function: { title: 'x', click: () => {} }.
  2. Resolve handlers by name from a registry when loading JSON config.
  3. Omit click entirely if you do not need click handling.

Example fix

// before
new nw.Tray({ title: 'x', click: 'onClick' });
// after
new nw.Tray({ title: 'x', click: onClick });
Defensive patterns

Strategy: type-guard

Validate before calling

if ('click' in option && typeof option.click !== 'function') {
  throw new TypeError('option.click must be a function');
}

Type guard

function isClickValid(o) { return !o.hasOwnProperty('click') || typeof o.click === 'function'; }

Prevention

When it happens

Trigger: new nw.Tray({ title: 'x', click: 'onClick' }) (string); new nw.Tray({ title: 'x', click: true }); click loaded from JSON config.

Common situations: JSON-deserialized config (no function type); referencing a handler by name string; setting click to a truthy non-function as a placeholder.

Related errors


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