nwjs/nw.js · error · TypeError
Must set 'title' or 'icon' field in option
Error message
Must set 'title' or 'icon' field in option
What it means
Thrown by the Tray constructor when option is an object but has neither a 'title' nor an 'icon' property. At least one of these must be present because the native Tray requires something to display (text or image). Both are checked via hasOwnProperty, so inherited properties do not count.
Source
Thrown at src/resources/api_nw_tray.js:29
var tray = trayEvents.objs[id];
if (!tray)
return;
var args = Array.prototype.slice.call(arguments, 1);
args.unshift('click');
tray.emit.apply(tray, args);
});
function Tray(option) {
if (!(this instanceof Tray)) {
return new Tray(option);
}
EventEmitter.apply(this);
if (typeof option != 'object')
throw new TypeError('Invalid option.');
if (!option.hasOwnProperty('title') && !option.hasOwnProperty('icon'))
throw new TypeError("Must set 'title' or 'icon' field in option");
if (!option.hasOwnProperty('title'))
option.title = '';
else
option.title = String(option.title);
if (option.hasOwnProperty('icon')) {
option.shadowIcon = String(option.icon);
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);View on GitHub (pinned to e15da848e9)
Solutions
- Include at least one of title or icon in the initial options: { title: '' } is acceptable (empty title) if you plan to set an icon immediately after.
- Default to a title or icon path in your config loader before constructing the Tray.
Example fix
// before
new nw.Tray({ tooltip: 'My App', click: handler });
// after
new nw.Tray({ title: 'My App', tooltip: 'My App', click: handler }); Defensive patterns
Strategy: validation
Validate before calling
if (!option || (!('title' in option) && !('icon' in option))) {
throw new TypeError("Tray option must include 'title' or 'icon'");
} Type guard
function hasTrayTitleOrIcon(o) { return !!o && (o.hasOwnProperty('title') || o.hasOwnProperty('icon')); } Prevention
- Always supply at least a title or an icon path.
- Default to an empty title (title: '') if you plan to set an icon immediately after.
- Validate optional config builders so at least one display field survives.
When it happens
Trigger: new nw.Tray({ tooltip: 'x' }) (only tooltip); new nw.Tray({}) (empty object); new nw.Tray({ click: fn }) (only a click handler).
Common situations: Building the options object from optional config where both title and icon ended up unset; intending to set the icon later via the setter but the constructor still requires one upfront; spreading a partial config.
Related errors
- A normal MenuItem must have a label
- Shortcut requires 'key' to specify key combinations.
- Must set 'title' or 'icon' field in option
- Shortcut requires 'key' to specify key combinations.
- Invalid option.
AI-assisted analysis of nwjs/nw.js@e15da848e9 (2026-08-13).
Data as JSON: /api/errors/1d6908f8235f7303.
Report an issue: GitHub.