Adophilus
Adophilus

Reputation: 310

NodeJS: How to fix "cannot find module 'node-windows' while compiling with pkg

While working on a particular project, I discovered I could compile my console applications with the pkg module. After installing it, I tested it out on my main app and it worked. When I tried it on "Service.js" (a service installer in the project) I got the warning (despite the fact that I have node-windows in the node_modules folder)

    Warning Cannot find module 'node-windows' from 'C:\Users\Uchenna\Documents\NodeJS\Ghost'

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

I tried ignoring it and went ahead to run "Service.exe --install"then I got this error:

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)

Here's the code in the Service.js file

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");
}

Upvotes: 3

Views: 3930

Answers (1)

HexaCrop
HexaCrop

Reputation: 4263

Certain packages when build using pkg is showing this error.

For me it was bcrypt.

Look into the error message and find out which executable the error message is referring to. Then copy that executable from node_modules folder (mostly it will be from this folder), and paste it along side the executable.

This resolved the issue for me

Upvotes: 2

Related Questions