Ad

How Can I Run Scripts In Served Pages In NW.js?

- 1 answer

I am trying to use nw.js to package a desktop application. the application navigates to pages that are being served from a xampp server on the same computer that the application will be running on.

When nw.js navigates to the page via file:// the scripts load succesfully.

When it navigates via http:// the scripts do not load.

the error in the console is:

Uncaught ReferenceError: require is not defined

How can I remedy this? I need it to load via http since the app uses php for a several essential functions.

Ad

Answer

Here is an example with node-remote.

package.json

{
  "main": "http://localhost:3030/",
  "name": "nw-demo",
  "description": "demo app of node-webkit",
  "version": "0.1.0",
  "node-remote" : "localhost:3030",
  "window": {
    "title": "node-webkit demo",
    "toolbar": true,
    "frame": true,
    "position": "center"
  }
}

html file on localhost

<script>document.write(require, process);</script>

nwjs (v0.12.3) app output

function (name) { if (name == 'nw.gui') return nwDispatcher.requireNwGui(); return global.require(name); }[object process]

Ad
source: stackoverflow.com
Ad