我正在尝试构建一个需要GRPC模块的应用程序。在命令提示符下运行时,我的应用程序运行得非常好-node server.js
但是,运行nwjs会崩溃并在下面抛出错误-
什么,我试过了-
一。如果我启用window.location.ref='http://localhost:3000';
然后nwjs windows显示localhost无法访问此站点localhost拒绝连接。并且console在显示错误后被清除,所以我注释了这一行以查看错误。(错误日志如下所示)

Uncaught Error: Failed to load gRPC binary module because it was not installed for the current system
Expected directory: node-webkit-v0.30.5-win32-x64-unknown
Found: [node-v57-win32-x64-unknown]
This problem can often be fixed by running "npm rebuild" on the current system
Original error: Cannot find module 'C:\Users\BLACK\Desktop\NWJS Xperiments\NuOS-OAuth\node_modules\grpc\src\node\extension_binary\node-webkit-v0.30.5-win32-x64-unknown\grpc_node.node'
    at Object.<anonymous> (C:\Users\BLACK\Desktop\NWJS Xperiments\NuOS-OAuth\node_modules\grpc\src\grpc_extension.js:53:17)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:719:10)
    at Module.load (internal/modules/cjs/loader.js:613:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:552:12)
    at Function.Module._load (internal/modules/cjs/loader.js:544:3)
    at Module.require (internal/modules/cjs/loader.js:650:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (C:\Users\BLACK\Desktop\NWJS Xperiments\NuOS-OAuth\node_modules\grpc\src\client_interceptors.js:145:12)
    at Module._compile (internal/modules/cjs/loader.js:702:30)

2.我已经手动创建了预期目录-“预期目录:node-webkit-v0.30.5-win32-x64-unknown”在grpc模块中,但现在我在dev工具中看到另一个错误,认为它已经解决了上述问题。
Uncaught Error: A dynamic link library (DLL) initialization routine failed.
\\?\C:\Users\BLACK\Desktop\NWJS Xperiments\NuOS-OAuth\node_modules\grpc\src\node\extension_binary\node-webkit-v0.30.5-win32-x64-unknown\grpc_node.node
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:749:18)
    at Module.load (internal/modules/cjs/loader.js:613:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:552:12)
    at Function.Module._load (internal/modules/cjs/loader.js:544:3)
    at Module.require (internal/modules/cjs/loader.js:650:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (C:\Users\BLACK\Desktop\NWJS Xperiments\NuOS-OAuth\node_modules\grpc\src\grpc_extension.js:32:13)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:719:10)
    at Module.load (internal/modules/cjs/loader.js:613:32)

三。我已经按照指示做了,但也没有运气。它又抛出了一组错误。
在尝试了第三点的说明之后,我得到了这个错误
> [email protected] install C:\Users\BLACK\Desktop\NWJS Xperiments\NuOS-OAuth\node_modules\grpc
> node-pre-gyp install --fallback-to-build --library=static_library

node-pre-gyp ERR! build error
node-pre-gyp ERR! stack Error: Failed to execute 'nw-gyp.cmd configure --fallback-to-build --library=static_library --module=C:\Users\BLACK\Desktop\NWJS Xperiments\NuOS-OAuth\node_modules\grpc\src\node\extension_binary\node-webkit-v1.11.3-win32-x64-unknown\grpc_node.node --module_name=grpc_node --module_path=C:\Users\BLACK\Desktop\NWJS Xperiments\NuOS-OAuth\node_modules\grpc\src\node\extension_binary\node-webkit-v1.11.3-win32-x64-unknown --napi_version=1 --node_abi_napi=napi' (Error: spawn nw-gyp.cmd ENOENT)
node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (C:\Users\BLACK\Desktop\NWJS Xperiments\NuOS-OAuth\node_modules\grpc\node_modules\node-pre-gyp\lib\util\compile.js:77:29)
node-pre-gyp ERR! stack     at emitOne (events.js:116:13)
node-pre-gyp ERR! stack     at ChildProcess.emit (events.js:211:7)
node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:196:12)
node-pre-gyp ERR! stack     at onErrorNT (internal/child_process.js:372:16)
node-pre-gyp ERR! stack     at _combinedTickCallback (internal/process/next_tick.js:138:11)
node-pre-gyp ERR! stack     at process._tickCallback (internal/process/next_tick.js:180:9)
node-pre-gyp ERR! System Windows_NT 10.0.17134
node-pre-gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\BLACK\\Desktop\\NWJS Xperiments\\NuOS-OAuth\\node_modules\\grpc\\node_modules\\node-pre-gyp\\bin\\node-pre-gyp" "install" "--fallback-to-build" "--library=static_library"
node-pre-gyp ERR! cwd C:\Users\BLACK\Desktop\NWJS Xperiments\NuOS-OAuth\node_modules\grpc
node-pre-gyp ERR! node -v v8.10.0
node-pre-gyp ERR! node-pre-gyp -v v0.10.0
node-pre-gyp ERR! not ok
Failed to execute 'nw-gyp.cmd configure --fallback-to-build --library=static_library --module=C:\Users\BLACK\Desktop\NWJS Xperiments\NuOS-OAuth\node_modules\grpc\src\node\extension_binary\node-webkit-v1.11.3-win32-x64-unknown\grpc_node.node --module_name=grpc_node --module_path=C:\Users\BLACK\Desktop\NWJS Xperiments\NuOS-OAuth\node_modules\grpc\src\node\extension_binary\node-webkit-v1.11.3-win32-x64-unknown --napi_version=1 --node_abi_napi=napi' (Error: spawn nw-gyp.cmd ENOENT)
npm WARN [email protected] No description
npm WARN [email protected] No repository field.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: `node-pre-gyp install --fallback-to-build --library=static_library`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\BLACK\AppData\Roaming\npm-cache\_logs\2018-05-23T01_42_49_740Z-debug.log

服务器.js
const express = require('express'),
    bodyParser = require('body-parser'),
    socket = require('socket.io'),
    BigTable = require('@google-cloud/bigtable'),
    PubSub = require('@google-cloud/pubsub'),
    passport = require('passport'),
    mongoose = require('mongoose'),
    cookieSession = require('cookie-session'),
    dotenv = require('dotenv').config(),
    config = require('./config/config'),
    gutils = require('./app/gutils'),
    authRoutes = require('./routes/auth-route'),
    passportSetup = require('./app/passport-setup'),
    app = express(),
    server = app.listen(3000),
    io = socket(server);

/*


App code removed


*/


app.get('/', (req, res) => {
    res.render('login');
});

app.use(express.static('public'));

console.log('Server running on the port 3000 ...');

另外,如果我在cmd中运行node server.js命令,然后运行nwjs,它会非常好地工作。
我正在Windows 10 x64上开发

最佳答案

我的方法会有点不同。首先,node.js应用程序和nwjs不能位于同一文件夹结构中,因为它们安装节点模块的方式不同。
你该怎么做。
创建一个基本的nwjs应用程序,如http://docs.nwjs.io/en/latest/For%20Users/Getting%20Started/
您可以选择最新版本,但请记住您拥有的版本,因为您需要知道GRPC的安装。
现在您必须安装要使用的grpc。

npm install grpc --build-from-source --runtime=node-webkit --target=0.32.2 --target_arch=x64

如1所述,请清楚您的NWJS版本。最近一次是0.32.2
并将目标拱设置为x64
现在一步一步地将代码复制到nwjs应用程序并运行它。nwjs与node.js应用程序不同。因此,分离node.js代码和nwjs代码非常重要。

09-10 00:03