NodeJS:如何修复"使用 pkg 编译时找不到模块'节点窗口'



在处理特定项目时,我发现可以使用 pkg 模块编译控制台应用程序。安装后,我在主应用程序上对其进行了测试,它工作了。当我在"Service.js"(项目中的服务安装程序)上尝试时,我收到了警告(尽管我在node_modules文件夹中有节点窗口)

Warning Cannot find module 'node-windows' from 'C:UsersUchennaDocumentsNodeJSGhost'

C:\Users\Uchenna\Documents\NodeJS\Ghost\Service.js

我尝试忽略它并继续运行"服务.exe--安装"然后我收到此错误:

Error: Cannot find module 'node-windows'
1) If you want to compile the package/file into executable, please pay attention to compilation warnings and specify a literal in 'require' call. 2) If you don't want to compile the package/file into executable and want to 'require' it from filesystem (likely plugin), specify an absolute path in 'require' call using process.cwd() or process.execPath.
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:592:15)
at Function.Module._resolveFilename (pkg/prelude/bootstrap.js:1280:46)
at Function.Module._load (internal/modules/cjs/loader.js:518:25)
at Module.require (internal/modules/cjs/loader.js:648:17)
at Module.require (pkg/prelude/bootstrap.js:1159:31)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (C:snapshotGhostService.js:0:0)
at Module._compile (pkg/prelude/bootstrap.js:1254:22)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:711:10)
at Module.load (internal/modules/cjs/loader.js:610:32)

这是服务.js文件中的代码

const Service = require("node-windows").Service;
class ServiceInstaller
{
constructor () {
var logServerService = new Service({
name: "Log Server Service",
description: "Listens for data to log",
script: "./DataLoggerServer.exe"
});
var mainService = new Service({
name: "Main Application Service",
description: "Logs data (or errors) to the Log Server Service",
script: "./Ghost.exe"
});
}
installServices () {
logServerService.install();
mainService.install();
logServerService.on("install", function () {
logServerService.start();
console.log("Successfully installed the 'Log Server' servicen");
});
mainService.on("install", function () {
mainService.start();
console.log("Successfully installed the main servicen");
});
}
uninstallServices () {
logServerService.uninstall();
mainService.uninstall();
logServerService.on("install", function () {
logServerService.start();
console.log("Successfully uninstalled the 'Log Server' servicen");
});
mainService.on("install", function () {
mainService.start();
console.log("Successfully uninstalled the main servicen");
});
}
}
var installer = new ServiceInstaller();
if (process.argv[2] == "install") {
installer.installServices();
}
else if (process.argv[2] == "uninstall") {
installer.uninstallServices();
}
else {
console.log("Not running...n");
}

使用 pkg 构建时的某些软件包显示此错误。

对我来说,这是隐秘的。

查看错误消息并找出错误消息所指的可执行文件。然后从node_modules文件夹中复制该可执行文件(大多数情况下它将来自此文件夹),并将其粘贴到可执行文件旁边。

这为我解决了问题