Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not work with node pkg build #54

Open
atulmahankal opened this issue Jan 16, 2023 · 1 comment
Open

not work with node pkg build #54

atulmahankal opened this issue Jan 16, 2023 · 1 comment

Comments

@atulmahankal
Copy link

service created with node-os-service for script successfully install and run.
but while I pack my project with vercel/pkg, It installs the service but while starting the service it gives the bellow error. What am I doing wrong?

Thanks in advance.

image

@lselden
Copy link

lselden commented Nov 6, 2023

Did you remember to call run? I haven't used os-service on windows for a while (I switched to custom wrapper around winsw), but FWIW here's what I've used in the past to make sure os-service is loaded properly (I put service.node binary in same folder as my executable):

// path_ is to make sure pkg's regex detects path.join(__dirname) properly (https://github.com/vercel/pkg#detecting-assets-in-source-code)
import path_ from 'path';

// try to ensure pkg includes os-service -- make sure relative path is correct
const path = path_;
path.join(__dirname, './node_modules/os-service');

let service = undefined;
async function lazyLoadService() {
    if (service) {
        return service;
    }
    const cwd = process.cwd();
    if (isPkg) {
        // include os-service only after running in the correct folder, to resolve service.node properly (though os-service expects it as ./build/Release/service
        process.chdir(path.dirname(process.execPath));
    }
    try {
        service = await import('os-service');
    } finally {
        if (isPkg) {
            process.chdir(cwd);
        }
    }
    return service;
}


/**
 * run a service
 * CALL THIS WHEN YOU FIRST START YOUR PROGRAM AS A SERVICE OTHERWISE WINDOWS WILL COMPLAIN
 * ...it's probably safe to call even if not running as a service
 */
export async function run(onStop = () => { }) {
    // throw if service.node couldn't be loaded
    await lazyLoadService();

    service.run(async () => {
        let returnCode = 0;
        try {
            if (typeof onStop === 'function') {
                await onStop();
            }
        } catch (err: any) {
            returnCode = parseInt(err.code, 10) || 1;
            throw err;
        } finally {
            service.stop(returnCode);
        }
    });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants