nwjs/nw.js · error · TypeError

Only menu of type "menubar" can be used as this.window menu

Error message

Only menu of type "menubar" can be used as this.window menu

What it means

Thrown by the NWWindow 'menu' setter when the assigned menu's type is not 'menubar'. Only a menubar-style nw.Menu can be set as a window's application menu; a contextmenu (popup) menu is rejected because it cannot back a window-level menu bar.

Source

Thrown at src/resources/api_nw_newwin.js:717

Object.defineProperty(NWWindow.prototype, 'isAlwaysOnTop', {
  get: function() {
    this.cWindow = currentNWWindowInternal.getCurrent(this.cWindow.id, {'populate': true});
    return this.cWindow.alwaysOnTop;
  }
});
Object.defineProperty(NWWindow.prototype, 'menu', {
  get: function() {
    var ret = privates(this).menu || {};
    return ret;
  },
  set: function(menu) {
    if(!menu) {
      privates(this).menu = null;
      currentNWWindowInternal.clearMenu(this.cWindow.id);
      return;
    }
    if (menu.type != 'menubar')
      throw new TypeError('Only menu of type "menubar" can be used as this.window menu');

    privates(this).menu =  menu;
    var menuPatch = currentNWWindowInternal.setMenu(menu.id, this.cWindow.id);
    if (menuPatch.length) {
      menuPatch.forEach((patch)=>{
        let menuIndex = patch.menu;
        let itemIndex = patch.index;
        let menuToPatch = menu.items[menuIndex];
        if (menuToPatch && menuToPatch.submenu) {
          menuToPatch.submenu.insert(new nw.MenuItem(patch.option), itemIndex);
        }
      });
    }
  }
});
Object.defineProperty(NWWindow.prototype, 'window', {
  get: function() {
    this.cWindow = currentNWWindowInternal.getCurrent(this.cWindow.id, {'populate': true});

View on GitHub (pinned to e15da848e9)

Solutions

  1. Construct the menu as menubar: `new nw.Menu({ type: 'menubar' })` before assigning.
  2. Set `win.menu = null` to clear an existing window menu.

Example fix

// before
const menu = new nw.Menu();
win.menu = menu;
// after
const menu = new nw.Menu({ type: 'menubar' });
win.menu = menu;
Defensive patterns

Strategy: type-guard

Validate before calling

if (menu && menu.type !== 'menubar') throw new TypeError('window menu must be type menubar');

Type guard

function isMenuBar(m) { return m && m.type === 'menubar'; }

Try / catch

try { win.menu = m; } catch (e) { if (/menubar/.test(e.message)) { win.menu = new nw.Menu({ type: 'menubar' }); } else throw e; }

Prevention

When it happens

Trigger: Running `win.menu = new nw.Menu({ type: 'contextmenu' })` or assigning a menu built without specifying type (defaults to contextmenu).

Common situations: Reusing the same context Menu instance for both a popup and the window menu; forgetting to set type: 'menubar' when constructing the application menu.

Related errors


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