Comments (5)
Not directly with this package, but your script can use a child process to execute any command it wants.
from node-windows.
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.
can you provide an example, how can I achieve this using scripts?
from node-windows.
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.
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)
- Needs windows restart before creating service HOT 1
- 03/30/2023: Please release a non-beta version HOT 2
- node-windows with babel and pkg HOT 1
- Question: Is it possible to split .exe creation and service creation HOT 1
- Comments in the XML file break the service executable
- Unsupported node: v19.0.0 HOT 1
- Event Logging other than event id 1000 HOT 7
- File path HOT 2
- 2023-07-13: React functionality HOT 1
- I need the source code for the binary files under "/bin" HOT 2
- Error while creating daemon folder: daemon already exists HOT 1
- [20231213]: Windows DNS integration HOT 2
- Noticed Typo in the documentation
- logpath property is not working HOT 2
- is it possible to have exe name different than the service name ?
- Why do we need to install it globally?
- March 6 2024 - Does this work with run-p
- Unable to execute child process HOT 1
- Each service is creating 2 seperate node processes HOT 4
- CPU overload due to wrapper launching new Node processes when an unhandled error occurs HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-windows.