npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@gaainf/node-service

v1.4.0

Published

Run console command as a service using NodeJS

Readme

build coverage License: MIT

node-service

Run console command as a service using NodeJS. You can start/stop console commands, test STDOUT, STDERR, wait specific conditions, control detached process parameters.

Prerequisites

This project requires NodeJS (version 10 or later) and npm or yarn.

Table of contents

Installation

Before you install: please read the prerequisites

Using npm:

$ npm install @gaainf/node-service

Using yarn:

$ yarn add @gaainf/node-service

Usage

Import

The package contains several classes:

  1. Service is base class to control console command execution, help start it in a detached process.
  2. Services is utility class to repeat console commands started in separate processes, help contol parameters of each of them.

Import Service

const {Service} = require('@gaainf/node-service');

Import Services

const {Services} = require('@gaainf/node-service');

Or import the both:

const {Service, Services} = require('@gaainf/node-service');

Service

Running console command as detached process

const {Service} = require('@gaainf/node-service');

let service = new Service();
service.start('node', ['-v']);
console.log(service.get_pid()); // process ID

Waiting for a command to complete in the background

const {Service} = require('@gaainf/node-service');

async () => {
    let service = new Service();
    service.start('PING', ['1.1.1.1', '-c', '3']);
    console.log(service.get_status());
    // timeout: 3 sec, delay: 1 sec
    await service.wait_condition(() => {return service.get_status() == 'finished'}, 3000, 1000); //boolean function, timeout, delay
    console.log(service.get_status()); // 'finished'
}();

Stopping command

const {Service} = require('@gaainf/node-service');

let service = new Service();
service.start('echo', ['Hello!']);
service.stop();
console.log(service.get_status()); // 'stopped'

Getting stdout and stderr

const {Service} = require('@gaainf/node-service');

(async () => {
    let service = new Service();
    service.start('node', ['-v']);
    await service.wait_condition(() => {return /v/.test(service.get_stdout())}, 1000);
    console.log(service.get_stdout()); // STDOUT
    service.start('node', ['-x']);
    await service.wait_condition(() => {return /bad option/.test(service.get_stderr())}, 1000);
    console.log(service.get_stderr()); // STDERR
})();

Parametrising input properties

Base input properties:

  • cwd - command path
  • args - command arguments
  • timeout - default timeout to test custom output expression (default value is 10 seconds)
  • delay - defualt delay between tries to test custom output expression (default value is 500 milliseconds)
const {Service} = require('@gaainf/node-service');

(async () => {
    let service = new Service({
        cwd: 'node',
        args: ['-e', 'setTimeout(function(){console.log("Hi!")},1000);'],
        timeout: 2000
    });
    service.start();
    await service.wait_condition(() => {return service.get_status() == 'finished'});
    console.log(service.get_stdout());
})();

Calculating duration

const {Service} = require('@gaainf/node-service');

(async () => {
    let service = new Service({cwd: 'node', args: ['-v']});
    service.start();
    await service.wait_condition(() => {return service.get_status() == 'finished'});
    console.log('Duration: ' + service.get_duration() / 1000 + ' sec');
})();

Services

Run several commands and wait until all conditions met

It is possible to run several comands in parallel and wait until all conditions met

const {Service, Services} = require('@gaainf/node-service');

(async () => {
    let services = new Services([
        {cwd: 'node', args: ['-v'], timeout: 1000},
        {cwd: 'node', args: ['--help'], timeout: 1000},
    ]);
    services.start_all();
    await services.wait_all_conditions([
        () => {return services.services[0].get_status() == 'finished'},
        () => {return services.services[1].get_status() == 'finished'}
    ]);
    for(let service of services.services) {
        console.log(service.get_stdout());
    }
    console.log("Duration time: " + services.get_duration());
})();

Repeat specific command until condition met

Following example runs HTTP server in separate process and repeats curl command until the server doesn't respond properly or timeout exceedded. At the end, it prints output data and calculates average duration.

const {Service, Services} = require('@gaainf/node-service');

(async () => {
    // simple HTTP server prints Hi on each request
    let service = new Service({
        cwd: 'node',
        args: [
            '-e', `
            const http = require("http");
            const listener = (req, res) => {res.end('Hi!')};
            http.createServer(listener).listen(8888, '127.0.0.1', () => {});`
        ]
    });

    // Check output via curl
    let curl = new Services({cwd: 'curl', args: ['-m1', 'http://127.0.0.1:8888/'], delay: 1000});
    try {
        // run HTTP server
        service.start();
        // repeat curl command every second
        await curl.repeat(() => {return /Hi!/.test(curl.get_stdout())});
    } finally {
        // be sure to stop the server 
        service.stop();
    }
    // print the last try stdout
    console.log(curl.get_stdout());
    //print average duration
    console.log("Duration time: " + curl.get_duration());
})();

Versioning

SemVer is used. For versions available, please, see tags on this repository.

Authors

  • Alex Grechin - Initial work - gaainf