Open Jc-L opened 7 years ago
Could you please provide a sample and steps to reproduce this?
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.
After putting package.json
and 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>
I can reproduce this issue on Windows with nwjs-sdk-v0.22.3. It works well on Linux.
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.
@jlangr did you start NW via pm2 when other require fails?
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.
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 arequire()
call:Problem: when starting this
nw.js
based application in an automated way withpm2
, it runs but the calls torequire()
fails with: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. Howeverpm2
may do other changes in the background, like closing some file descriptors, etc.Similar posts refer to a missing
"node-remote"
entry inpackage.json
. I added such entry but without any impact.How can I make
require()
calls work in all circumstances, including whenpm2
starts the application ? If not a known problem, how can I investigate further to identify why require() is not present ?