nwjs/nw.js · error · TypeError

Shortcut requires 'key' to specify key combinations.

Error message

Shortcut requires 'key' to specify key combinations.

What it means

Thrown by the Shortcut constructor when option is an object but has no truthy 'key' property (!option.key). The key is the accelerator string (e.g. 'ctrl+a') and is the single required field; without it the Shortcut cannot be bound to a hardware combination.

Source

Thrown at src/resources/api_nw_shortcut.js:158

}

function unregisterLocal(shortcut) {
  var localKey = normalizeLocal(shortcut._accelerator);
  delete handlers[localKey];
}

function Shortcut(option) {
  if (!(this instanceof Shortcut)) {
    return new Shortcut(option);
  }

  EventEmitter.apply(this, arguments);

  if (typeof option != 'object')
    throw new TypeError(OPTION_INVALID);

  if (!option.key)
    throw new TypeError(OPTION_KEY_REQUIRED);

  if (option.hasOwnProperty('active')) {
    if (typeof option.active != 'function')
      throw new TypeError(OPTION_ACTIVE_INVALID);
  }

  if (option.hasOwnProperty('failed')) {
    if (typeof option.failed != 'function')
      throw new TypeError(OPTION_FAILED_INVALID);
  }

  var self = this;

  this.on('active', function() {
    if (!self.active) return;
    if (typeof self.active != 'function')
      throw new TypeError(OPTION_ACTIVE_INVALID);
    self.active.apply(self, arguments);

View on GitHub (pinned to e15da848e9)

Solutions

  1. Ensure the options object includes a non-empty key string, e.g. { key: 'ctrl+a' }.
  2. Validate that option.key is a truthy string before calling new nw.Shortcut().

Example fix

// before
new nw.Shortcut({ active: handler });
// after
new nw.Shortcut({ key: 'ctrl+a', active: handler });
Defensive patterns

Strategy: validation

Validate before calling

if (!option || typeof option.key !== 'string' || option.key.length === 0) {
  throw new TypeError('Shortcut requires a non-empty key string');
}

Type guard

function hasShortcutKey(o) { return !!o && typeof o === 'object' && typeof o.key === 'string' && o.key.length > 0; }

Prevention

When it happens

Trigger: new nw.Shortcut({ active: fn }) (key omitted); new nw.Shortcut({ key: '' }) (empty string is falsy); new nw.Shortcut({ key: null }); new nw.Shortcut({}) (empty object).

Common situations: Building the options object conditionally where the key assignment was skipped; destructuring or spreading that dropped the key field; passing a config object intended for a different API.

Related errors


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