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.prototype 'menu' setter (win.menu = menu) when menu is truthy and menu.type !== 'menubar'. A window-level menu must be a menubar-style menu (the kind that renders as a native top menu bar); context menus ('contextmenu' type) or menus without a type cannot be attached to a window. Assigning a falsy value clears the menu and is allowed.

Source

Thrown at src/resources/api_nw_window.js:631

    });
    Object.defineProperty(NWWindow.prototype, 'isAlwaysOnTop', {
      get: function() {
        return this.appWindow.isAlwaysOnTop();
      }
    });
    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();
          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);
        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() {
        return this.appWindow.contentWindow;

View on GitHub (pinned to e15da848e9)

Solutions

  1. Construct the menu with type: 'menubar': const m = new nw.Menu({ type: 'menubar' }); then win.menu = m.
  2. To clear the window menu, assign win.menu = null (falsy values are allowed and clear it).
  3. Keep separate Menu instances for context menus vs window menus.

Example fix

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

Strategy: type-guard

Validate before calling

if (menu && menu.type !== 'menubar') {
  throw new TypeError('win.menu requires a menubar-type nw.Menu');
}
win.menu = menu;

Type guard

function isWindowMenu(m) { return !m || (m instanceof nw.Menu && m.type === 'menubar'); }

Prevention

When it happens

Trigger: win.menu = new nw.Menu({ type: 'contextmenu' }); win.menu = new nw.Menu() (default type is not menubar); win.menu = aMenuObjectMissingType.

Common situations: Reusing a context menu instance for the window menu; forgetting to set type: 'menubar' when constructing the Menu; assuming the default Menu type is menubar (it is not).

Related errors


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