Node.js Command Prompt Issue - node.js

I don't believe this behavior is correct at all, but please correct me if I'm mistaken. So I have an Angular 2/Ionic 2 app created all through the Node.js command prompt... the commands all work fine, up until I execute ng serve, after that I can't type at all into the command prompt. Only way I can type is if I close out of the command prompt and restart (less than ideal). Is this normal behavior? Or only occurring on my machine?

ng serve launches a basic web server for you using which your static files are served. It also listens for updates to your project files and if any changes are detected it bundles the project again and reloads the browser tab automatically. So it's important that it keeps running when you're developing your application.

Related

How to properly run NodeJs on Windows Server in production

I am having difficult times, trying to make my NodeJS scripts run on windows server 2012. Or more precisely, to make it robust.
I have installed PM2, whic his great, also added service for windows startup which works fine, but now I found biggest issue I can't solve.
When windows server user start pm2 start, directly on server or through ssh, when logging out, all pm2 scripts are gone.
I've tried to look into pm2-windows-service but that seems inconsistent, when I restart service, it works fine, but sometimes I need to manually reload o restart only 1 script and then whole list of pm2 scripts gets somehow detached or attached to user, so when I log out from server it's all gone again.
I can't find solution to have watcher/autorestart on scripts, and make them run as a service regardless of user being logged in/out.
There must be solution for running multiple nodejs scripts on windows ?
Once you launch your NodeJS processes, you should run pm2 save command. That way, your stated scripts are saved.
Your attached/dettached problems may be due a relative path configuration in the service env variables. Try moving to an absolute path.
Note: PM2 recommendation is to launch commands with an admin-privileged user; otherwise inconsistences may appear.
Note (2): Bare in mind that PM2 on Windows has some issues. For example, if you restart your server, most of the times processes dissapear from pm2 ls (they noted that Windows does not have the feature to restore saved instances). If you saved them, pm2 resurrect will restore them.

How to use Command Prompt after ng serve was compiled

In Angular 5, after creating project folder, installing Angular CLI, Node.js (all latest versions) and ng serve was compiled successfully through command prompt. Then I tried to use command prompt to install bootstrap. I could not control / use command prompt. What might be the cause and effect?
Command prompt stuck after compilation
There are simple ways:
If you use Command Prompt(Terminal), open another Command Prompt inside your project path.
If you use IDE like Visual Studio Code, open Terminal and then you can use Plus(+) sign for openning anothor Terminal like below image:
If you use IDE like WebStrom (Jetbrains IDE), first open IDE Terminal then make right click and select New Session and new terminal openning like below image:
There are couple of ways you can do that.
The official way of deployment of angular app.
Create a start.bat (if you are using windows) and write the ng serve --open command there. Now you can run this file which will internally run your angular app, using forever or pm2 npm modules.
If you do that these will demonise your process to run in background and your same command prompt becomes usable again.
Once the process is finished( compiled successfully), you would notice that the cmd -prompt newline doesn't show any directory and its blank.
This means that, you just have to open an another (new terminal), get to the right directory using "cd command "and implement your desired commands or operations again.
use ctrl + v it will ask weather to terminate say Y, cursor will go back

How to run a node.js file as a background process on a Windows server?

I was creating a node.js project and uploaded it to my Windows server to provide an API service for mobile application.
When I open command prompt and type
node app.js
It runs correctly, but when I close the command prompt my node.js server stopped running.
How to make it still running when I close the commend prompt?
For example on Ubuntu I use the command
nohup
How can I do this on Windows?
You can make the process run in background using pm2
pm2 start app.js --watch
This will start the process and will also look for changes in the file.
More about watch flag
Nodemon #ftw. On Windows, Forever doesn't really watch files so much as casually observe them, while pm2 restarts the server every time you load a page.
Each of these tools works similarly, and each installs just as in the accepted answer. E.g.:
npm install nodemon -g
This installs nodemon globally, and to use you can simply navigate to your project folder and enter:
nodemon
(Assuming your project has an app.js file). You can add the -w switch, just as in Forever and pm2, however if you're just wanting to watch all files, you can omit that. To run nodemon in the background with Forever, you would run this:
forever nodemon --exitcrash
Nodemon is good for development, especially on Windows, while Forever and pm2 are more for production, on Linux.
Here is a simpler answer that cuts right to the chase without any added libraries or overhead like in the other two answers described above. To run your Node.js application as a windowless startup program in the background (this would be analogous to "nohup" in Linux), modify this template to suit and copy it into a .VBS script file. Then copy that file to your Start Menu startup folder (for all users, that would be C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup) and it will automatically run. The techniques you are using here in Visual Basic are (1) preparing to run the Node.js application by first changing the working directory of the shell object and (2) informing the shell to run the Node.js application in a hidden window by adding a “, 0” immediately after the run function:
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.CurrentDirectory = "C:\path-to-your-node-js-app\"
objShell.Run("""node"" your-app.js"), 0
Set objShell = Nothing
References:
https://keestalkstech.com/2016/07/start-nodejs-app-windowless-windows/
https://devblogs.microsoft.com/scripting/how-can-i-change-the-working-folder-of-a-script/
No, you can't.
Even if you make a GUI program you'll need to run it via console.
And as soon as you close the command prompt. Your service would be stopped/ terminated that moment only. Because node creates a server itself while running : http.createServer().listen(port) or app.listen(port). So this this makes it independent in nature.
So, as soon as you close the command prompt on which server was running all the services would stop at that moment.

Node.js open in a new console without pause

I'm very newby in node.js world, and I'm doing the first steps, but I can't step forward because when I try to do any operation from node.js command prompt in Windows 10, the node.js console is opened and closed very fast and I haven't time enough to read the errors or anything that is written on console. Is there any way to configure node.js to stop the console before to quit it? Or to execute on same opened Node.js command prompt.
For example I'm unable to read the version of different modules installed, the console opens and closes very fast.
I've tried windows standard command line, node.js command prompt, an application called cmder, and in all instructions related to node it throws a new window with node.exe. If the command waits user prompt the console (node.exe) is paused, but when I try an application that only log some data (like npm --version) I can't see the result, because after log, the console is closed.
Some time ago, I've tried in Windows 7, and I remember that the node prompt was opened on the same command console. I don't know if it's the SO, or the node.js version (4.4.4 LTS).
Ouch! I've found the solution, I was trying to avoid the annoying "run as administrator" dialog and I've configured the node application properties checking the flag "Compatibility > Run This Program As An Administrator". That was the source of all my problems!
Hope it helps anyone!

running a server file on node.js

I've been practicing with "hello world" examples of websockets and node.js server.
According to all those examples you create a html file (client) and a js file (server).
Before you run them, you have to run this on the command line (I use windows)
node nameOFtheServer.js
So, my question. If I close the command line window and open it again the client does not connect to the server. I have to run again the above code in the command line , manually, so the server will start again. Why is this happening? Is that normal? How can I fix it , so I dont have to run the same commands over and over again on the command line in order to start the js file (server) ?
Thanks
EDIT
OK, new facts, I just edited the question, highlighting the changes in Italics
When you close the terminal, everything that runs in it is killed. There are many solutions on both Linux and Windows systems, most of them create some sort of a service which then runs in the background.
Here are some possible solutions:
http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever
https://github.com/indexzero/daemon.node
http://www.coretechnologies.com/products/AlwaysUp/Apps/RunNodeJSAsAService.html
http://coreybutler.github.io/node-windows/manual/#!/api/nodewindows.Service
Pick the one that is best for you.
Related question on StackOverflow:
How to run node.js app forever when console is closed?
First of all thanks Venemo for your anser. I tried use the forever module, but did not worked well, as you can see here.
So I decided to use nssm with node.js
I download the nssm and unzip it in the C:Program Files\path\to\nodejs. And then I opened Window's command window and typed C:\program files\path\to\nssm-2.16\win32 and then typed nssm.exe. You should see a "menu" how to install or remove services. And now type
"C:\Program Files\path\to\nssm.exe" install give-Your-Service-A-Name "C:\path\to\node.exe" \"C:Program Files\nodejs\path\to\yourServerFile.js"
Notice the \ before the "C:Program Files\nodejs\path\to\yourServerFile.js" it's not a typo, you should type it, is important, if you have spaces in your path, helps nssm to interpret correctly.
And that's it, now press CTRL+ALT+DEL, open the Services tab, and find give-Your-Service-A-Name , right click and select Start service. To check, open your client file that communicates with the yourServerFile.js, it should be working, without having to start the yourServerFile.js from command line.
(PS : I use nodejs 0.10.12 and nssm 2.16 on windows 7. The instructions above are a combination of this tutorial and this anser)

Resources