nwjs/nw.js · error · String
It's forbidden to instantialize a Base class.
Error message
It's forbidden to instantialize a Base class.
What it means
Base is the abstract root class for every nw.js API object (App, Menu, MenuItem, Clipboard, Screen, Shortcut). Its constructor unconditionally throws, so `new (require('nw.gui').Base)()` never produces an instance. Note the thrown value is `new String(...)`, a String object rather than an Error — so `instanceof Error` is false in a catch block. The message also contains a typo ("instantiate").
Source
Thrown at src/api/base/base.js:24
// in the Software without restriction, including without limitation the rights
// 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 Base() {
throw new String("It's forbidden to instantialize a Base class.");
}
require('util').inherits(Base, require('events').EventEmitter);
// Silent unhandled events
Base.prototype.handleEvent = function() {
this.emit.apply(this, arguments);
}
// Generic getter and setter
Base.prototype.handleGetter = function(name) {
return v8_util.getHiddenValue(this, 'option')[name];
}
Base.prototype.handleSetter = function(name, setter, type, value) {
value = type(value);
v8_util.getHiddenValue(this, 'option')[name] = value;
nw.callObjectMethod(this, setter, [ value ]);
}View on GitHub (pinned to e15da848e9)
Solutions
- Do not instantiate Base; use a concrete subclass (nw.App, new nw.Menu(), new nw.MenuItem(opt), new nw.Shortcut(opt)).
- If subclassing, give your type a constructor that calls nw.allocateObject(this, option) before inheriting from Base.
- If you only wanted EventEmitter behavior, inherit from require('events').EventEmitter instead of nw.Base.
Example fix
// before
var b = new nw.Base(); // throws
// after
var menu = new nw.Menu({ type: 'contextmenu' }); // concrete subclass Defensive patterns
Strategy: validation
Validate before calling
// Base is abstract — never construct it. Validate intent before any nw.Base use:
if (obj === nw.Base || (obj && obj.constructor === nw.Base)) {
throw new Error('Use a concrete nw API class, not Base.');
} Type guard
function isAbstractBase(v) {
return v === undefined || (v && v.constructor && v.constructor.name === 'Base');
} Try / catch
try { var x = new nw.Base(); }
catch (e) {
// Note: thrown value is a String object, not an Error
if (typeof e === 'object' && e instanceof String && /forbidden/.test(String(e))) {
console.error('Do not instantiate nw.Base; use a concrete class.');
} else { throw e; }
} Prevention
- Treat nw.Base as internal; never reference it in app code.
- When subclassing, always provide a constructor that calls nw.allocateObject.
- For EventEmitter-only behavior, inherit from require('events').EventEmitter.
When it happens
Trigger: Directly instantiating Base: `new nw.Base()`. Indirectly: a subclass whose constructor forgets to allocate the native object and falls through, or code that reaches Base via a misconfigured prototype chain. Also possible if a user attempts to subclass nw API classes and calls Base.call(this).
Common situations: Developers exploring the API surface try to construct Base directly, or copy-paste the inheritance pattern (`require('util').inherits(MyClass, nw.Base)`) without providing their own constructor that calls nw.allocateObject. Inheritance misuse during framework authoring.
Understand the failure class
- Authentication and authorization failures — expired tokens, bad credentials, and missing scopes.
Related errors
- Menu.items is immutable
- 'type' is immutable at runtime
- Invaild parameter, need Shortcut object.
- Type of '{type}' is not supported
- Only support getting plain text from Clipboard
AI-assisted analysis of nwjs/nw.js@e15da848e9 (2026-08-13).
Data as JSON: /api/errors/798cd2722b71e9b6.
Report an issue: GitHub.