nwjs/nw.js · error · TypeError

'active' must be a valid function.

Error message

'active' must be a valid function.

What it means

Thrown by the nw.Shortcut constructor when the 'active' option key is present but its value is not a function. nw.Shortcut binds a global key accelerator and invokes 'active' on registration success, so it must be callable. The typeof check rejects strings, objects, numbers, and undefined-after-coercion values.

Source

Thrown at src/api/shortcut/shorcut.js:35

// 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 Shortcut(option) {
  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) {

View on GitHub (pinned to e15da848e9)

Solutions

  1. Ensure option.active is a function: `new nw.Shortcut({ key, active: () => {} })`.
  2. If you only want failure handling, omit 'active' entirely and provide 'failed' instead.
  3. If loading handlers dynamically, resolve the string name to a real function reference before constructing the Shortcut.

Example fix

// before
new nw.Shortcut({ key: 'Ctrl+A', active: 'onActive' });
// after
new nw.Shortcut({ key: 'Ctrl+A', active: onActive });
Defensive patterns

Strategy: type-guard

Validate before calling

if (option && option.hasOwnProperty('active') && typeof option.active !== 'function') {
  throw new Error('option.active must be a function');
}

Type guard

function isShortcutOption(o) {
  return o && typeof o === 'object' &&
    typeof o.key !== 'undefined' &&
    (!o.hasOwnProperty('active') || typeof o.active === 'function') &&
    (!o.hasOwnProperty('failed') || typeof o.failed === 'function');
}

Try / catch

try { new nw.Shortcut(opt); } catch (e) { if (e instanceof TypeError) console.warn('Bad shortcut option:', e.message); else throw e; }

Prevention

When it happens

Trigger: Calling `new nw.Shortcut({ key: 'Ctrl+Shift+A', active: 'someString' })`, passing an arrow stored in a variable that is actually undefined, or passing `active: { handle: fn }` instead of the function itself.

Common situations: Passing a method reference from an object that hasn't been initialized; deserializing shortcut config from JSON where functions become strings; migrating from a config that stored handler names instead of references.

Related errors


AI-assisted analysis of nwjs/nw.js@e15da848e9 (2026-08-13). Data as JSON: /api/errors/34f46a4d647da04c. Report an issue: GitHub.