nwjs/nw.js · error · TypeError

Invalid option.

Error message

Invalid option.

What it means

Thrown by the Shortcut constructor when the option argument is not an object (typeof option != 'object'). This catches primitive arguments, undefined, and null — the constructor requires an option object that at least carries a 'key' property. Note that typeof null === 'object' in JS, so null is NOT caught here and will instead fail later at the !option.key check.

Source

Thrown at src/resources/api_nw_shortcut.js:155

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

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;

View on GitHub (pinned to e15da848e9)

Solutions

  1. Pass an options object: new nw.Shortcut({ key: 'ctrl+a', active: fn }).
  2. If you have a bare key string, wrap it: new nw.Shortcut({ key: myKeyString }).

Example fix

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

Strategy: type-guard

Validate before calling

if (typeof option !== 'object' || option === null) {
  throw new TypeError('Shortcut option must be a non-null object');
}

Type guard

function isShortcutOption(o) { return typeof o === 'object' && o !== null && !Array.isArray(o); }

Prevention

When it happens

Trigger: new nw.Shortcut('ctrl+a') (passing a string instead of an options object); new nw.Shortcut() (no argument, option is undefined); new nw.Shortcut(42) (number).

Common situations: Migrating from an API that accepted a bare key string; forgetting the options wrapper; passing a variable that was never assigned.

Related errors


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