nwjs/nw.js · error · TypeError

Invalid option.

Error message

Invalid option.

What it means

Shortcut's constructor requires its argument to be an object (`typeof option != 'object'` → TypeError). This mirrors MenuItem's option check. As with MenuItem, `typeof null === 'object'`, so null slips past this guard and fails later when `.hasOwnProperty('key')` is called on null.

Source

Thrown at src/api/shortcut/shorcut.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
// all 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 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

View on GitHub (pinned to e15da848e9)

Solutions

  1. Pass an options object: `new nw.Shortcut({ key: 'Ctrl+A', active: fn })`.
  2. If you only have a key string, wrap it: `new nw.Shortcut({ key: str })`.
  3. Do not pass null; supply a real object with at least a `key` property.

Example fix

// before
new nw.Shortcut('Ctrl+A'); // throws
new nw.Shortcut();         // throws

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

Strategy: validation

Validate before calling

function buildShortcut(opt) {
  if (opt == null || typeof opt !== 'object' || Array.isArray(opt))
    throw new TypeError('Shortcut requires an options object');
  return new nw.Shortcut(opt);
}

Type guard

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

Try / catch

try { return new nw.Shortcut(opt); }
catch (e) {
  if (e instanceof TypeError && /Invalid option/.test(e.message)) {
    return new nw.Shortcut({ key: String(opt) }); // coerce a raw key string
  } throw e;
}

Prevention

When it happens

Trigger: Calling new nw.Shortcut(), new nw.Shortcut('Ctrl+A'), or new nw.Shortcut(undefined). Passing the key string directly instead of an options object is the most common case.

Common situations: Developers assume the constructor takes the key string positionally. API examples that show `{key}` in docs being misread as taking a string. Migration from libraries with positional constructors.

Related errors


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