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
- Pass a function: { title: 'x', click: () => {} }.
- Resolve handlers by name from a registry when loading JSON config.
- 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
- Pass a function reference for click.
- Resolve handler names from JSON config before construction.
- Omit click if you do not need click handling.
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
- 'active' must be a valid function.
- 'failed' must be a valid function.
- Invalid option.
- Must set 'title' or 'icon' field in option
- 'menu' must be a valid Menu
AI-assisted analysis of nwjs/nw.js@e15da848e9 (2026-08-13).
Data as JSON: /api/errors/3008e602dc762cd9.
Report an issue: GitHub.