Control NW.js Desktop Windows from JavaScript
Use NW.js's nw.Window API to open, minimize, and track desktop windows from renderer code without native addons.
04 Jun 2026, 09:56 UTC

The problem: your settings window stays closed
You are building a desktop application with NW.js (formerly Node-WebKit). The main window loads, but you need a second window for settings, a way to minimize the main window from a menu button, and a log message when the user closes the app. You do not want to write a C++ addon or set up heavy inter-process communication just to move a window.
What nw.Window gives you
NW.js exposes a global nw object in the renderer process. The renderer process is the Chromium context that runs your HTML, CSS, and client-side JavaScript. Through nw.Window, you can inspect and control browser windows from that same JavaScript context. Most inspection and control calls are synchronous, so you do not need promises or IPC round-trips to minimize, focus, or close a window.
nw.Window.get() returns the window object for the current page. You can attach listeners to events such as closed, minimize, maximize, and restore. To open a new window, use nw.Window.open(), which accepts a URL, window options, and a callback that receives the new window object.
Declare default window behavior in package.json
Before the app launches, NW.js reads the window field in package.json to set the initial size, position, resizability, and taskbar visibility. This is configuration, not code.
{
"name": "desktop-tool",
"main": "index.html",
"window": {
"title": "Desktop Tool",
"width": 900,
"height": 600,
"resizable": true,
"show": true,
"taskbar": true
}
}
Change width and height to match your design. Set show to false if you want to create the window hidden and reveal it programmatically after setup.
Worked example: main window and settings window
API surface and security defaults change between major NW.js versions, so verify this example against your installed release before copying it into production.
Add buttons to index.html:
<button id="open-settings">Open settings</button>
<button id="minimize-main">Minimize</button>
Run this script in your renderer context:
const mainWin = nw.Window.get();
mainWin.on('minimize', () => {
console.log('Main window minimized');
});
mainWin.on('closed', () => {
console.log('Application window closed');
});
document.getElementById('minimize-main').addEventListener('click', () => {
mainWin.minimize();
});
document.getElementById('open-settings').addEventListener('click', () => {
nw.Window.open('settings.html', {
width: 400,
height: 300,
resizable: false,
show: true
}, (settingsWin) => {
settingsWin.on('closed', () => {
console.log('Settings window closed');
});
});
});
Clicking Minimize calls mainWin.minimize() directly. Clicking Open settings launches settings.html in a new 400 by 300 window and logs when that window closes.
Verify before you ship
- Open the NW.js developer tools for your app.
- In the console, run
typeof nw.Window. Expectfunction. - Launch the app with the NW.js binary and confirm the window opens at the
package.jsonwidth and height. - Click the Minimize button and confirm the window minimizes on your host operating system.
Run terminal commands from the directory containing your package.json. No special permissions are required, but launching the NW.js binary as root is unnecessary and increases risk if the app later loads untrusted content.
Trade-offs and limitations
- Version drift: NW.js pairs specific Chromium and Node.js releases. Window behavior and security defaults change between major versions. Check the release notes for your target version before relying on a specific event or flag.
- Renderer risk: Because Node.js is available in the renderer context, loading untrusted remote content expands the attack surface. Do not open arbitrary URLs without sandboxing or content restrictions.
- Platform differences: Minimize-to-tray, always-on-top, and taskbar behavior differ on Windows, macOS, and Linux. Test the minimize and restore flow on each target OS.
Closing
Start with package.json window defaults, use nw.Window.get() for the current window, and use nw.Window.open() for secondary windows. Verify with the developer console and a manual minimize test on each operating system you support. If your app loads third-party content, treat the renderer as a trust boundary and review NW.js sandbox options before shipping.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.