nwjs/nw.js · error · TypeError
Invalid option
Error message
Invalid option
What it means
Thrown by the nw.Tray constructor when the option argument is not an object (typeof check). Tray requires an options object because it must read at least 'title' or 'icon' from it. Primitives, null in some engines, and undefined all fail this guard.
Source
Thrown at src/api/tray/tray.js:25
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
// pies of the Software, and to permit persons to whom the Software is furnished
// to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in al
// l copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH
// ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
var v8_util = process.binding('v8_util');
function Tray(option) {
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 = nw.getAbsolutePath(option.icon);
}
if (option.hasOwnProperty('alticon')) {
option.shadowAlticon = String(option.alticon);
option.alticon = nw.getAbsolutePath(option.alticon);
}View on GitHub (pinned to e15da848e9)
Solutions
- Pass an options object: `new nw.Tray({ title: 'My App' })`.
- Default the argument: `new nw.Tray(options || { title: 'App' })`.
- Add a typeof guard before construction to surface a clearer upstream error.
Example fix
// before
const tray = new nw.Tray(appTitle);
// after
const tray = new nw.Tray({ title: appTitle }); Defensive patterns
Strategy: validation
Validate before calling
if (typeof option !== 'object' || option === null) option = {}; Type guard
function isTrayOption(o) { return o && typeof o === 'object' && (o.hasOwnProperty('title') || o.hasOwnProperty('icon')); } Try / catch
try { new nw.Tray(opt); } catch (e) { if (/Invalid option/.test(e.message)) opt = { title: 'App' }; else throw e; } Prevention
- Always construct the options object explicitly before passing it.
- Default the argument: opt = opt || {}.
When it happens
Trigger: Calling `new nw.Tray()`, `new nw.Tray(null)`, `new nw.Tray('My Tray')`, or `new nw.Tray(42)`.
Common situations: Forgetting to pass options; passing a string title directly as in older tray APIs; passing a variable that was conditionally assigned and ended up undefined.
Related errors
- 'click' must be a valid Function
- 'menu' must be a valid Menu
- 'active' must be a valid function.
- 'failed' must be a valid function.
- Must set 'title' or 'icon' field in option
AI-assisted analysis of nwjs/nw.js@e15da848e9 (2026-08-13).
Data as JSON: /api/errors/c60556480a349fbc.
Report an issue: GitHub.