nwjs / nw.js

Call all Node.js modules directly from DOM/WebWorker and enable a new way of writing applications with all Web technologies.
https://nwjs.io
MIT License
40.32k stars 3.88k forks source link

require() not defined in some cases #5934

Open Jc-L opened 7 years ago

Jc-L commented 7 years ago

Version: nw.js 0.22.3 OS: Windows 7_64

The package.json contains "main": "app/index.html", and this file contains a script section with a require() call:

var seneca = require('seneca')({tag: 'desktopnotification'});

Problem: when starting this nw.js based application in an automated way with pm2, it runs but the calls to require() fails with:

 "Uncaught ReferenceError: require is not defined", source: chrome-extension://whatever/app/index.html

This error does not happen when the application is started directly by hand.

So far, one difference I identified is the running nw.js process argument: --nwapp-path is "." when it runs properly, and the absolute path to the app when it fails. However pm2 may do other changes in the background, like closing some file descriptors, etc.

Similar posts refer to a missing "node-remote" entry in package.json. I added such entry but without any impact.

How can I make require() calls work in all circumstances, including when pm2 starts the application ? If not a known problem, how can I investigate further to identify why require() is not present ?

Christywl commented 7 years ago

Could you please provide a sample and steps to reproduce this?

Jc-L commented 7 years ago

Please find hereunder a package.json and index.html. This package reproduce an issue leading to the require() symbol not available when the service is started through pm2. Note that pm2 must usually be installed globally on your system and is not part of the dependencies.

Steps to reproduce

After putting package.jsonand index.html files in one folder:

npm install
npm install -g pm2

And from them, compare both cases (adjust nw.exe PATH, here I use git bash), the working one:

 '/C/Program Files/nwjs-sdk-v0.22.3-win-x64/nw.exe' --enable-logging=stderr .

and the failing one:

pm2 start '/C/Program Files/nwjs-sdk-v0.22.3-win-x64/nw.exe' --name 'nwjs-5934' -- --enable-logging=stderr .

package.json

{
  "name": "nwjs-5934",
  "version": "0.0.1",
  "main": "index.html",
  "node-remote": "http://localhost",
  "description": "An attempt to reproduce the 'missing require()' error",
  "repository": "No repository yet",
  "license": "MIT",
  "dependencies": {
    "nw": "^0.22.3",
    "ansi-regex": "^2.1.1"
  },
  "devDependencies": {
  },
  "window": {
    "title": "nwjs-5934",
    "show_in_taskbar": false,
    "toolbar": true,
    "show": true,
    "position": "center",
    "fullscreen": false,
    "icon": ""
  }
}

index.html

<html>
<head>
<title>nwjs #5934</title>
<style>
* {font-family: Verdana,sans-serif;}
</style>
<script>
function doLoad() {
  console.log("before require() call");
  const ansiRegex = require('ansi-regex');
  console.log("after require() call");
}
</script>
</head>
<body>
<button onclick="doLoad()" style="height: 80px;width: 300px;color:white;font-size: 22px;background: cadetblue;border: none;cursor: pointer;">
  Do require() call</button>
  <div>See debug window to see if require() call works</div>
</body>
</html>
Christywl commented 7 years ago

I can reproduce this issue on Windows with nwjs-sdk-v0.22.3. It works well on Linux.

jlangr commented 5 years ago

I have this problem still. The sample code works--doing a require of 'os' works but any other require (e.g. lodash) fails. I tried installing lodash globally and locally.

This is on nw 0.34.5.

rogerwang commented 5 years ago

@jlangr did you start NW via pm2 when other require fails?

jlangr commented 5 years ago

thanks @rogerwang for the response. Sorry--I did not start it via pm2. This is "in development" i.e. while trying to learn about nw.