{"record":{"id":"55910edb41076222","repo":"nwjs/nw.js","slug":"a-normal-menuitem-must-have-a-label","errorCode":null,"errorMessage":"A normal MenuItem must have a label","messagePattern":"A normal MenuItem must have a label","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/api/menuitem/menuitem.js","lineNumber":40,"sourceCode":"\nfunction MenuItem(option) {\n  if (typeof option != 'object')\n    throw new TypeError('Invalid option.');\n\n  if (!option.hasOwnProperty('type'))\n    option.type = 'normal';\n\n  if (option.type != 'normal' &&\n      option.type != 'checkbox' &&\n      option.type != 'separator')\n    throw new TypeError('Invalid MenuItem type: ' + option.type);\n\n  if (option.type == 'normal' || option.type == 'checkbox') {\n    if (option.type == 'checkbox')\n      option.checked = Boolean(option.checked);\n\n    if (!option.hasOwnProperty('label'))\n      throw new TypeError('A normal MenuItem must have a label');\n    else\n      option.label = String(option.label);\n\n    if (option.hasOwnProperty('icon')) {\n      option.shadowIcon = String(option.icon);\n      option.icon = nw.getAbsolutePath(option.icon);\n    }\n\n    if (option.hasOwnProperty('iconIsTemplate'))\n      option.iconIsTemplate = Boolean(option.iconIsTemplate);\n    else\n      option.iconIsTemplate = true;\n\n    if (option.hasOwnProperty('tooltip'))\n      option.tooltip = String(option.tooltip);\n\n    if (option.hasOwnProperty('enabled'))\n      option.enabled = Boolean(option.enabled);","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/nwjs/nw.js/blob/e15da848e9e08e6e467532dae78995c6ad2f55ee/src/api/menuitem/menuitem.js#L22-L58","documentation":"For types 'normal' and 'checkbox', the constructor enforces that a `label` property exists (via hasOwnProperty) before coercing it with String(). A normal/checkbox item without a label throws a TypeError. Separator type is exempt because it takes a different branch that discards all other options.","triggerScenarios":"Calling new nw.MenuItem({ type: 'normal' }) with no label, or { type: 'checkbox', checked: true }) without label. Setting label to undefined explicitly ({label: undefined}) also throws because hasOwnProperty('label') is true but the value is undefined.","commonSituations":"Building an icon-only menu item (the API does not support label-less normal items). Forgetting the label when adding a checkbox. Refactoring that strips the label field conditionally.","solutions":["Add a `label` property to the option: `new nw.MenuItem({ type: 'normal', label: 'Open' })`.","If you want a blank label, pass an empty string explicitly ({ label: '' }).","For divider items, use type: 'separator' instead."],"exampleFix":"// before\nnew nw.MenuItem({ type: 'normal', icon: 'open.png' }); // throws\n\n// after\nnew nw.MenuItem({ type: 'normal', label: 'Open', icon: 'open.png' });","handlingStrategy":"validation","validationCode":"function buildItem(opt) {\n  if ((!opt.type || opt.type === 'normal' || opt.type === 'checkbox') &&\n      !opt.hasOwnProperty('label')) {\n    opt.label = ''; // or a real label\n  }\n  return new nw.MenuItem(opt);\n}","typeGuard":"function itemHasRequiredLabel(opt) {\n  var needsLabel = !opt.type || opt.type === 'normal' || opt.type === 'checkbox';\n  return !needsLabel || opt.hasOwnProperty('label');\n}","tryCatchPattern":"try { return new nw.MenuItem(opt); }\ncatch (e) {\n  if (e instanceof TypeError && /must have a label/.test(e.message)) {\n    opt.label = opt.label || '';\n    return new nw.MenuItem(opt);\n  } throw e;\n}","preventionTips":["Always set a label for normal/checkbox items (empty string is allowed).","Use type 'separator' for dividers.","Validate required fields before construction."],"tags":["nwjs","menuitem","constructor","validation","required-field"],"backgroundTag":null,"analyzedSha":"e15da848e9e08e6e467532dae78995c6ad2f55ee","analyzedAt":"2026-08-13T04:15:35.452Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}