Giter VIP home page Giter VIP logo

Comments (5)

coreybutler avatar coreybutler commented on June 17, 2024 1

Not directly with this package, but your script can use a child process to execute any command it wants.

from node-windows.

miquelfire avatar miquelfire commented on June 17, 2024 1

Full example for running a NPM script as a service

install.js

var Service = require('node-windows').Service;

// Create a new service object
var svc = new Service({
  name: 'Hello World',
  description: 'The nodejs.org example web server.',
  script: 'C:\\Path\\To\\Your\\Project\\Root\\hello-world\\app.js', 
  env: {
    name: "NODE_ENV",
    value: "production"
  }
});

// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install', function() {
  svc.start();
});

// Just in case this file is run twice.
svc.on('alreadyinstalled', function() {
  console.log('This service is already installed.');
});

// Install the script as a service.
svc.install();

runnpm.js

let child_process = require('child_process');
child_process.execSync('npm start',{stdio:[0,1,2]});

I don't know child_process enough to give an example, but you could edit runnpm.js to multiple NPM commands.

from node-windows.

AkashRajvanshi avatar AkashRajvanshi commented on June 17, 2024

can you provide an example, how can I achieve this using scripts?

from node-windows.

miquelfire avatar miquelfire commented on June 17, 2024

For script, you would make a .js (or .mjs or .cjs) file for the service to run, and use child process to run the NPM command

Non ESM version I just pulled from a random SO answer:

var child_process = require('child_process');
child_process.execSync('npm start',{stdio:[0,1,2]});

from node-windows.

AkashRajvanshi avatar AkashRajvanshi commented on June 17, 2024

Is this correct? Can you please provide a full example? I need to run multiple Node services in the background using npm.

var Service = require('node-windows').Service;
var child_process = require('child_process');

// Create a new service object
var svc = new Service({
  name: 'Hello World',
  description: 'The nodejs.org example web server.',
  script: 'C:\\Path\\To\\Your\\Project\\Root\\hello-world\\app.js', 
  env: {
    name: "NODE_ENV",
    value: "production"
  }
});

// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install', function() {
  svc.start();
});

// Just in case this file is run twice.
svc.on('alreadyinstalled', function() {
  console.log('This service is already installed.');
});

// Listen for the "start" event and let us know when the
// process has actually started working.
svc.on('start', function() {
  console.log(svc.name + ' started!\nVisit http://127.0.0.1:3000 to see it in action.');
  
  // Run npm run start with node-windows
  console.log("Running npm run start with node-windows.");
  child_process.execSync('npm run start', { stdio: [0,1,2] });
});

// Install the script as a service.
svc.install();

from node-windows.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.