How to add npm start as service in windows - node.js

I have nodejs project and need to run my node project as windows service. I tried nssm but it is not working. I often use linux . Is there another way to run "npm start" as service in windows without using nssm.

Related

How can Nodejs apps always run as a Windows service?

I have windows Server 2016 cloud hosting. I broadcast my sites on IIS.
I have 2 apps running on Nodejs. I log out of windows after configuring my applications.
My IIS sites continue to run, but my nodejs applications quit when I log out or restart windows.
I have followed the steps below to fix this issue.
I installed the pm2 library. Globally.
npm install --global pm2#latest
I installed the pm2-windows-service library. Globally.
npm i pm2-windows-service -g
I installed the pm2-windows-startup library. Globally.
npm install pm2-windows-startup -g
I uninstall running services to avoid running apps repeatedly.
pm2 kill
If there are any running services, I first uninstall it.
pm2-service-uninstall
I installed the pm2-service-install library. I named it "pm2Service1". I see it started when I enter services.msc.
pm2-service-install -n pm2Service1
I start the startup service for applications to start when Windows opens.
pm2-startup install
I go to the directory where the application is installed and run the application with pm2. (If I don't enter the directory and run it, it gives an error. Can't read the sql / file.sql file.)
c:
cd C:\webSites\myService1
pm2 start app.js
I check the application with list and show, and see if it works.
pm2 list
pm2 show 0
After making sure it works.
I'm recording running applications.
pm2 save --force
When I do these procedures and log off windows, I can access the application from outside.
But when I restart windows the application does not work. I have to repeat the same steps every time it starts up.
When Windows restarts, "pm2Service1" seems to be working. But pm2 list the lists as empty.
I accomplished this on my servers by using NSSM Service. Once you install it, go to NSSM folder and type
nssm install PM2
You can find the entire tutorial here:
https://lakin-mohapatra.medium.com/register-pm2-as-a-service-in-the-windows-server-747f19e2ff2a
Best practice AFAIK is using nssm + "windows batch file" to start your app.js, for more details you can check this link.

Linking Windows commands inside Windows Subsystem for Linux Ubuntu

I'm doing web development on Windows 10 using Windows Subsystem for Linux with Ubuntu 18.04. Using Node.js and NPM inside Ubuntu and running dev servers and API servers works.
When I am inside WSL I can run commands:
npm i
node app
npm run serve
# etc...
And now there is a new situation. I have a project using Nightmare.js (improved but similar to Phantom.js) which will install Electron headless browser when running npm i. Running npm i inside WSL will install Ubuntu version of Electron and when trying to run nothing will happen, browser will not be open. Which is logical because WSL Ubuntu has no visual environment thus can't open a browser in it.
If I run npm i with PowerShell or CMD (npm that is installed on Windows instead WSL will be used), Windows version of Electron will be installed and I can run it with node app inside PowerShell and it will work as expected, browser opens etc...
This creates and interesting precedent. It looks like I will need to sometimes run my applications with Windows commands. Instead of doing npm i or node app directly in WSL I have to switch to CMD or PowerShell and execute them there, so that windows versions of npm and node are used instead of WSL Ubuntu's. This is not very convenient and I would like to do it all from WSL. For an example of this behavior there is Visual Studio Code from Microsoft. With Remote-WSL add-on installed I can run code command inside WSL Ubuntu and VSCode will open in Windows.
Here is my question: Is there a way to link commands in WSL Ubuntu to Host Windows system just like it is done with VSCode? Ideally I would like to have something like this in my WSL Ubuntu: windows-npm i, windows-node app that will run npm and node on the Host Windows System instead of the same commands on WSL Ubuntu.
There is little to no attention to the issue, so I decided to fix my self. I'm still looking for better/native solution but here is a workaround I came up with.
WSL-Link
Allows WSL users to run any CMD commands on host Windows system from within linux subsystem.
Requirements
Windows 10
WSL
Node.js
NPM
Node and NPM have to be installed both in linux subsystem and on Windows host system.
Install
wsl-link has to be installed separately in subsystem and on Windows host
On subsystem linux:
npm i wsl-link -g
On Windows host:
npm i wsl-link -g
Windows Startup
To run script at startup on windows I use PM2 with supplied daemon script.
Install pm2 and pm2-windows-startup on Windows host:
npm i pm2 pm2-windows-startup -g
Install pm2-startup:
pm2-startup install
Run wsl-link pm2 daemon:
wsl-link-pm2
Save pm2 list of processes
pm2 save
You can now confirm that the wsl-link app is running, with:
pm2 status
Usage
On Windows start server (if not using startup setup with pm2):
wsl-link
Use on subsystem linux (npm -v will be run on host Windows):
wsl-link npm -v
Case
On subsystem linux setup a project (or use existing):
mkdir wsl-link-test
cd wsl-link-test
touch app.js
npm init
app.js:
const Nightmare = require('nightmare');
(async () => {
await Nightmare({
show: true
})
.goto('https://google.com');
})();
Install Windows version of Nightmare.js and run it on Windows.
wsl-link npm i nightmare --save
wsl-link node app
You should see Electron browser open on your Windows host.

reactjs app not run after serve and start

I just installed node.js on my centos 7 server, then install react
it successfully created my new react app, after that, I run:
npm start
and or serve -s build
but none of these run in my browser.
This site can’t be reached
how can I solve this?
If you have sever inside can you try with start debugging of your application
Stop npm, if you're using yarn
Start the project with command above, inside your project home folder
yarn start
Take some time to learn commands below:

I am having issues while deploying react and node application on unix standalone server

I have developed my application on a windows machine and have to deploy this on a standalone UNIX server and while running commands like: npm start, it says that package is not available but I checked and everything is available in the project's npm modules.
Do I need to use docker or how can I achieve that without any container such as docker or kubernete.
Can I try cloning my repo in some other Linux machine and npm install and then use the same folder to deploy on UNIX CentOS running on the server?

How to run the Reactjs Web Application in localhost:3000 in windows

I'm a new React Developer, and I'm having a problem in running it to my chrome. I already did the npm install in cmd, and then I npm start by its not starting because of error in cmd. And my OS is Windows 10.
What should I do?
in your package.json file the script define rm command witch is not available in windows that's why you are getting error
insted of rm change it to del
and if there is any UNIX specific command then change it to windows equivalent

Resources