coreybutler / node-windows

Windows support for Node.JS scripts (daemons, eventlog, UAC, etc).
Other
2.78k stars 357 forks source link

Execute npm start #334

Closed pols63 closed 1 year ago

pols63 commented 1 year ago

Hi: Is it possible to execute "npm start" or "npm run script" with this package? Like that:

var svc = new Service({ name:'Hello World', description: 'The nodejs.org example web server.', script: 'npm start', });

Thanks!

coreybutler commented 1 year ago

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

AkashRajvanshi commented 1 month ago

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

miquelfire commented 1 month ago

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]});
AkashRajvanshi commented 1 month ago

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();
miquelfire commented 1 month ago

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.