nwjs/nw.js · error · TypeError
'failed' must be a valid function.
Error message
'failed' must be a valid function.
What it means
Thrown by the nw.Shortcut constructor when the 'failed' option is present but is not a function. 'failed' is the callback invoked when the accelerator cannot be registered (e.g. the key combo is taken by the OS), so it must be callable.
Source
Thrown at src/api/shortcut/shorcut.js:42
if (typeof option != 'object')
throw new TypeError('Invalid option.');
if (!option.hasOwnProperty('key'))
throw new TypeError("Shortcut requires 'key' to specify key combinations.");
option.key = String(option.key);
this.key = option.key;
if (option.hasOwnProperty('active')) {
if (typeof option.active != 'function')
throw new TypeError("'active' must be a valid function.");
else
this.active = option.active;
}
if (option.hasOwnProperty('failed')) {
if (typeof option.failed != 'function')
throw new TypeError("'failed' must be a valid function.");
else
this.failed = option.failed;
}
v8_util.setHiddenValue(this, 'option', option);
nw.allocateObject(this, option);
}
require('util').inherits(Shortcut, exports.Base);
Shortcut.prototype.handleEvent = function(ev) {
if (ev == 'active') {
if (typeof this.active == 'function')
this.active();
} else if (ev == 'failed') {
if (typeof this.failed == 'function')
this.failed(arguments[1]);
}View on GitHub (pinned to e15da848e9)
Solutions
- Provide a real function for 'failed' or omit it entirely.
- Guard with typeof before passing: `if (typeof cb === 'function') opt.failed = cb;`.
Example fix
// before
new nw.Shortcut({ key: 'Ctrl+A', failed: false });
// after
new nw.Shortcut({ key: 'Ctrl+A', failed: (err) => console.error(err) }); Defensive patterns
Strategy: type-guard
Validate before calling
if (option && option.hasOwnProperty('failed') && typeof option.failed !== 'function') {
throw new Error('option.failed must be a function');
} Type guard
function isCallable(v) { return typeof v === 'function'; } Try / catch
try { new nw.Shortcut(opt); } catch (e) { if (/failed/.test(e.message)) logger.warn(e.message); else throw e; } Prevention
- Omit 'failed' when you do not need error handling rather than passing a placeholder.
- Resolve handler references before building the option object.
When it happens
Trigger: Calling `new nw.Shortcut({ key: 'Ctrl+A', failed: null })`, `failed: false`, or `failed: someObj.method` where `this` binding produced undefined.
Common situations: Copy-paste from a config that set failed to a boolean flag; passing a value from an optional field that defaulted to a non-function.
Related errors
- 'active' must be a valid function.
- Invalid option.
- Shortcut requires 'key' to specify key combinations.
- Invalid option
- 'click' must be a valid Function
AI-assisted analysis of nwjs/nw.js@e15da848e9 (2026-08-13).
Data as JSON: /api/errors/3fe4cd20b85806fb.
Report an issue: GitHub.