nwjs/nw.js · error · TypeError
Invalid option.
Error message
Invalid option.
What it means
Thrown by the Tray constructor when the option argument is not an object (typeof option != 'object'). The constructor requires an options object carrying at least a 'title' or 'icon' field. Note typeof null === 'object' so null bypasses this check and fails later at the hasOwnProperty calls.
Source
Thrown at src/resources/api_nw_tray.js:26
trayEvents.clickEvent = bindingUtil.createCustomEvent("NWObjectTrayClick", false, false);
trayEvents.clickEvent.addListener(function(id) {
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);
}View on GitHub (pinned to e15da848e9)
Solutions
- Pass an options object: new nw.Tray({ title: 'My App' }) or new nw.Tray({ icon: 'icon.png' }).
- Ensure the argument is defined and an object literal before construction.
Example fix
// before
new nw.Tray('My App');
// after
new nw.Tray({ title: 'My App' }); Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof option !== 'object' || option === null) {
throw new TypeError('Tray option must be a non-null object');
} Type guard
function isTrayOption(o) { return typeof o === 'object' && o !== null && !Array.isArray(o); } Prevention
- Pass an object literal to new nw.Tray().
- Type-check external config before construction.
- Remember null is typeof 'object' — exclude it explicitly.
When it happens
Trigger: new nw.Tray('My App') (string); new nw.Tray() (undefined); new nw.Tray(123) (number); passing a pre-ES5 primitive wrapper.
Common situations: Assuming the constructor takes a title string directly; migrating from an API with a simpler signature; passing a variable that was conditionally assigned.
Related errors
- Invalid menu type: {option.type}
- Invalid option.
- Invalid MenuItem type: {option.type}
- A normal MenuItem must have a label
- Invalid option.
AI-assisted analysis of nwjs/nw.js@e15da848e9 (2026-08-13).
Data as JSON: /api/errors/e72ca88f63a7d6dc.
Report an issue: GitHub.