nwjs/nw.js · error · TypeError

Invalid option.

Error message

Invalid option.

What it means

MenuItem's constructor requires its argument to be an object. `typeof option != 'object'` throws a TypeError for strings, numbers, booleans, undefined, and functions. Note that JS quirk: `typeof null === 'object'`, so passing null bypasses this guard and will instead fail later on `null.hasOwnProperty` — a different error.

Source

Thrown at src/api/menuitem/menuitem.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 al
// l 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 MenuItem(option) {
  if (typeof option != 'object')
    throw new TypeError('Invalid option.');

  if (!option.hasOwnProperty('type'))
    option.type = 'normal';

  if (option.type != 'normal' &&
      option.type != 'checkbox' &&
      option.type != 'separator')
    throw new TypeError('Invalid MenuItem type: ' + option.type);

  if (option.type == 'normal' || option.type == 'checkbox') {
    if (option.type == 'checkbox')
      option.checked = Boolean(option.checked);

    if (!option.hasOwnProperty('label'))
      throw new TypeError('A normal MenuItem must have a label');
    else
      option.label = String(option.label);

View on GitHub (pinned to e15da848e9)

Solutions

  1. Pass an options object: `new nw.MenuItem({ type: 'normal', label: 'Open' })`.
  2. If you only have a label string, wrap it: `new nw.MenuItem({ label: str })`.
  3. Never pass null; either omit the argument or supply a real object.

Example fix

// before
new nw.MenuItem('Open'); // throws
new nw.MenuItem();      // throws

// after
new nw.MenuItem({ label: 'Open' });
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

try { return new nw.MenuItem(option); }
catch (e) {
  if (e instanceof TypeError && /Invalid option/.test(e.message)) {
    return new nw.MenuItem({ label: String(option) });
  } throw e;
}

Prevention

When it happens

Trigger: Calling new nw.MenuItem(), new nw.MenuItem('label'), new nw.MenuItem(42), or new nw.MenuItem(undefined).

Common situations: Developers assume the constructor takes a label string (like some other UI libraries). Forgetting the option object entirely. Migrating from an API that accepted positional or string args.

Related errors


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