Search code examples
javascriptnode-webkit

How to set .js file as main in NW.js?


I followed NW.js' offical doc, but the window never appears.
If I switch package.json to "main": "index.html", window appears. but if I return to "main": "main.js", window doesn't appear.

This is my main.js:

var nw = require('nwjs');

nw.Window.open("index.html", {}, function(win) {});

I have to set "main": "main.js" because a module I want to use doesn't support .html file as "main".
Does anyone have a solution?


Solution

  • I'd be curious what module requires your main to be a JS file. It's pretty rare that you'd have an NW.js project that doesn't use an html file as the main (I strongly recommend using "main": "index.html").

    Your problem is that var nw = require('nwjs'); is equivalent to doing nw = undefined. window.nw and global.nw are both already accessible at all times in the default context. You are basically deleting the thing you need.

    index.html

    <!DOCTYPE html>
    <html>
      <head>
        <title>TEST</title>
      </head>
      <body>
        <h1>Test<h1>
      </body>
    </html>
    

    index.js

    nw.Window.open('index.html');
    

    package.json

    {
      "name": "test",
      "version": "1.0.0",
      "main": "index.js",
      "scripts": {
        "start": "nw ."
      },
      "devDependencies": {
        "nw": "0.51.0-sdk"
      },
      "author": "Julien",
      "description": "Test",
      "license": "MIT"
    }
    

    Then just npm install && npm start. But again, you don't want this, you want "main": "index.html", it's just a lot less trouble.