nw.js: nwjs taskbar icon on Windows for NW.js is a red square, can't set icon in package.json

NWJS Version : 0.42.5 SDK version Operating System : Windows 10 (x64)

Expected behavior

You should be able to set the icon associated with NW.js using the provided method of setting “icon” in the “window” section of the package.json file.

Actual behavior

The icon associated with the NW.js application is a red square and cannot be set using the provided methods in the package.json. See screenshot in the comments below.

How to reproduce

Download 0.42.5 SDK for Windows x64 and set your package.json to be like:

NOTE: This is not a path problem as it works fine in 0.40.

{
  "name": "app-name-here"
  "main": "index.html",
  "window": {
    "icon": "image.png"
  }
}

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 33 (10 by maintainers)

Commits related to this issue

Most upvoted comments

I managed to reproduce it in another system. This is now fixed in git and will be available in the next nightly build.

interesting … it works for me with win10 and the default app. Could you try to clear the icon cache? https://www.winhelponline.com/blog/how-to-rebuild-the-icon-cache-in-windows/#script

If I switch between 0.42.3 and 0.42.5, icon work in 0.42.3 and doesn’t work in 0.42.5, so it’s not icon cache (and users won’t clear it)

Same just now with clean base, only extracted. I tested both 32 and 64 bit version and both SDK and non-SDK versions with same results.

Windows 10 Pro, version 1903, build 18362.476

image

image

I don’t think a sample is really needed of my icon because the same icon works fine in NW.js 0.40.0 and in much older releases. I’ve been using the same icon for my app for a lot of years.

Here is my icon:

toby

Just running NW.js demonstrates the behavior. Here is a screenshot of it. Notice that the task bar icon is a red square. When you attempt to set the icon in the package.json it doesn’t work.

image

📚 The icon guide to fix everything

Or i hope most of the issues you could encounter like me.

🪟 Change window icon (top-left)

image Either directly into the html, or in your package.json by adding the following key:

// package.json
{
    // ...
    "window": {
        "icon": "path.png"  // Relative path are from the NW.exe location. Image cannot be .ico
                            // Image needs to be a perfect square to not be streched.
                            // However, too large images are slower to load so consider putting 128px to 1024px size instead of a 240MB 4K full HD image-
        // "toolbar": true,         // Not necessary
        // "width": 1000,           // Not necessary
        // "height": 800,           // Not necessary
        // "position": "center",    // Not necessary
        // "resizable": true        // Not necessary
    },
    // ...
}

💻Change taskbar icon

image You can add the following key in your package.json file:

// package.json
{
    // ...
    "icons": {
        "256": "path.png" // Relative path are from the NW.exe location. Image cannot be .ico
        // Image doesn't need to be 256px, but needs to be a perfect square to not be streched.
        // However, too large images are slower to load so consider putting 128px to 1024px size instead of a 240MB 4K full HD image-
    },
    // ...
}

📲 Change executable icon

image By using Ressource Hacker you will be able to change the icon of the file. Tutorial here: https://www.youtube.com/watch?v=QD1xJjhEypg After editing the icon, you might not see it directly. To refresh the icon cache of windows without restarting your computer you can open a Powershell terminal and run ie4uinit -show

🖥️ Change Tray icon (bottom right of your desktop)

image Simply add in your script

// if nw is undefined use require("nw.gui") instead
var tray = new nw.Tray({
    title: 'Tray name', // Mac only as Windows displays natively the icon
    tooltip: 'Hover text', // In my example picture its "POG Cardinal 2"
    icon: "path.png", // Relative path are from the NW.exe location. Image cannot be .ico
});

🟥 Workarounds red square on new opened windows

image

Best fix

The best workaround i’ve found that far is this one, from this issue. Basically, in your script you’ll redefine how new opened windows should be instancied, and then pass it to the NW function which ensure a window opened with the icon (and actually stacked like Chrome onto each others). image

require('nw.gui').Window.get().on('new-win-policy',function(frame, url, policy){
    policy.ignore();
    // The lext line open a new window with the red square issue
    // chrome.windows.create({url:"https://google.com",type:"normal",focused:true});
    //The next line open a new window stacked with the main one.
    require('nw.gui').Window.open(url,{frame:true,focus:true}); // {frame:true,focus:true} is not necessary
});

You can also use it in standalone directly with using require('nw.gui').Window.open("your url"); instead of open(url). But the code above also fixes the new pages opened by the <a>link</a> tags.

Other fix

You can add the following into your package.json, but keep in mind that this is not intended to fix this bug specifically. As written, you’ll be disabling the nw2 features, which can lead to potential other issues. Use at your own risk.

// package.json
{
    // ...
    "chromium-args": "--disable-features=nw2"
    // ...
}

❔ Sources

I am still facing this issue while developing my application.

Scenario: My application launches multiple new windows depending on the condition set by the users. I used “window.open(url, ‘_blank’);” which is an existing logic that I can’t change.

To set the common height, width and icon for all windows I used the below logic:

currWin.on(‘new-win-policy’, function(frame, url, policy) { policy.setNewWindowManifest({ ‘icon’: ‘…/assets/app.png’, ‘frame’: true, ‘width’: 1300,
‘height’: 900,
‘position’: ‘center’, }); });

The above is working fine on OSX, but when runs on Windows 10 I am still getting a red icon for the newly open window. Please refer to the below screenshot. Screenshot 2020-12-03 at 6 17 36 PM

When I use --disable-features=nw2 it works.

Using: nw.js v0.50.1 Screenshot 2020-12-03 at 6 17 16 PM