![](/img/trans.png)
[英]How to fix "cannot find module'./node'' while compiling with pkg
[英]NodeJS: How to fix "cannot find module 'node-windows' while compiling with pkg
在處理特定項目時,我發現我可以使用 pkg 模塊編譯我的控制台應用程序。 安裝后,我在我的主應用程序上對其進行了測試,並且可以正常工作。 當我在“Service.js”(項目中的服務安裝程序)上嘗試時收到警告(盡管我在 node_modules 文件夾中有節點窗口)
Warning Cannot find module 'node-windows' from 'C:\Users\Uchenna\Documents\NodeJS\Ghost'
C:\\Users\\Uchenna\\Documents\\NodeJS\\Ghost\\Service.js
我嘗試忽略它並繼續運行“Service.exe --install”然后我收到了這個錯誤:
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:\snapshot\Ghost\Service.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)
這是 Service.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' service\n");
});
mainService.on("install", function () {
mainService.start();
console.log("Successfully installed the main service\n");
});
}
uninstallServices () {
logServerService.uninstall();
mainService.uninstall();
logServerService.on("install", function () {
logServerService.start();
console.log("Successfully uninstalled the 'Log Server' service\n");
});
mainService.on("install", function () {
mainService.start();
console.log("Successfully uninstalled the main service\n");
});
}
}
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 構建時的某些軟件包顯示此錯誤。
對我來說是 bcrypt。
查看錯誤消息並找出錯誤消息所指的可執行文件。 然后從 node_modules 文件夾中復制該可執行文件(主要來自該文件夾),並將其粘貼到可執行文件旁邊。
這為我解決了這個問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.