Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
114 views
in Technique[技术] by (71.8m points)

javascript - 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?

question from:https://stackoverflow.com/questions/65936261/how-to-set-js-file-as-main-in-nw-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...