how to do nodejs hot deployment in nodeclipse , enide studio, express server? - node.js

I am debugging an application in nodeclispe/ enide studio. I wish to change js files on the go while the express server is running, instead of doing a server restart. HOw can I do that?

Since you are debugging an application the best you can do is use the module nodemon, this module watches your folder for files change when a file changes the application is automatically redeployed.
To install nodemon use this command, install it globally:
npm install nodemon -g
To execute from CLI you use
nodemon --debug app.js
PS: Have in mind that when i say redeployed the memory of the process is flushed and do not use nodemon under production environment.

Related

Is it okay to do an npm install on another terminal window while nodemon is running?

Let's say I am working on an app that listens on port 3000. Nodemon is running. I require another npm package in the app.js file, and on another terminal window that is open to the directory of that app, I run npm install (package name).
Will this cause any issues?
I've just tested this case on Node.js project and turns out that installing additional library while nodemon was running from the secondary terminal doesn't cause any issues. After installation was complete, nodemon picked up changes and reloaded application.

How to use a local version of pm2 in node_modules directory to keep a server alive?

I want to keep my node server alive. Therefore I use pm2 but if I try to start my server with
pm2 start index.js
I get the message:
pm2: command not found
So, I wanted to ask how to use local pm2 in node_modules directory without installing pm2 globally.
Do I have to register pm2 in my index.js?
Can anyone provide some information about the command to start a server via pm2 which is locally installed?
Actually you can install it as a local project dependency and then run it directly with the path inside node_modules: node ./node_modules/pm2/bin/pm2 start
That way you can use it in a npm script for example.
I use this for a project that needs to run offline, bundling everything in the CI and running it locally then.
However as of lately I get some problems with the deamon starting under Windows that way. Not yet sure if it is a Windows problem or a problem with starting pm2 this way (as the globally installed version still works properly).
Try,npx pm2 start index.js. Read this article to learn about npx
If you are on AWS EC2 instance you can run the command from this path:
C:\Users\Administrator\AppData\Roaming\npm\pm2 start C:\project\app.js
In my case pm2 was installed but the err was pm2 npt found
so i ran pm2 command from that path and i worked
If you use npm, simply write in your package.json / "scripts":
"pm2": "npx pm2 start index.js -i max"
then run the script with npm run pm2
it will start your index.js with max available cluster workers

React with Express application using "pm2 start" always shows status as errored

I have a React app with Node as the server and I am writing my code in ES6 and transpiled using babel. I am using Windows OS. I was using NodeMon for watching changes it worked fine in development. I am making a production configuration for the app and thought of using PM2 for both dev and prod.
Below is the script in my package.json,
"scripts": {
"dev": "pm2 start --watch --interpreter babel-node src/server/server.js --name appDev",
...
"prod": "pm2 start build/server/server.js -i max --name appProd"
}
I have ES6 code in my src folder and transpiled ES5 code in build folder. "prod" command works fine if I remove "-i max" [infinite command prompts started opening up with "-i max". Hope it will be fine in server after deployment].
Now the problem is, "dev" script is always resulting in "errored" status and when I check status error logs are empty.
ANy idea what's going on? I will run NodeMon in dev and PM2 in prod for now but why PM2 has issue with ES6 and --interpreter babel-node?
Update:
I have tried --interpreter babel-cli after installing globally, it failed with an error [PM2][ERROR] Interpreter babel-cli does not seem to be available. I have babel-cli globally and locally.
Even pm2 start app.js --interpreter ./node_modules/.bin/babel-node didn't work.
I had issues deploying with the latest (12.16.2) version of node. I was having issues with ES6 and imports. I understand there are more complex solutions that can solve that issue, but backing up to node 12.4.0 solved that issue for me. NOTE: Node 12.4.0 is an earlier release than 12.16.2. I picked version 12.4.0 because that is what Digital Oceans "Node" servers are initialized with. I figured that their server guys probably know far more about it than I, and just decided to cheat off their test, so to speak.
I am having no issues with 12.4.0, and you can easily download an earlier version off of the node website. If you plan to install it on a Linux server, I would recommend using nvm. I had used nvm to go back to 12.4.0, and still had issues (I am sorry I forget specifics). I just redid the server from a fresh image with plain Ubuntu 18.04 LTS and am good on that at this time. Even with the local Windows install of node that I use to develop, I had to do a full uninstall and a clean install to avoid issues. Highly recommend this route if able.
Good luck.

How can I automatically restart a Node.js application using Forever and Nodemon (Windows)

I am running a node.js application in Windows and I want to make it automatically restart if there is an unhandled exception in the code which causes the application to stop.
I have done some research and I found that a combination "Forever" and "Nodemon" can achieve this goal.
I installed both packages globally on my Windows 10 Device.
npm install forever -g
npm install -g nodemon
I tried using the following command to launch my app:
forever start nodemon --exitcrash app.js
However, I get the following error: "nodemon does not exist"
If try just running "nodemon" the application starts which indicates the Nodemon package is installed however, this will not allow the app to restart after a crash.
Am I doing something wrong? Most advice I find online is only relevant to Linux systems.
If you are already using forever, then you can get rid of nodemon. Instead you can use a combination of forever and cluster module. Simply fork the worker in case of exceptions, and it makes your app more scalable too!
If still nodemon is preferable, maybe try installing it globally using the -g flag
Forever and nodemon achieve 2 completely different objectives
nodemon is used to run your application in development mode, where you are frequently changing code, and need to restart the server .It will not restart your application in case of a crash. more about that later
Forever, on the other hand, is for making your application run as a daemon in production. And auto restart if you have uncaught exceptions.
Historically people have used Forever stand alone, or with upstart scripts, running as a linux service one of the most famous being upstart
Current norm is to use PM2

sailsjs live update during serverside editing

It's annoying to have to restart the sails server when you change something, is there any way to make sailsjs do what meteor does where when you save a serverside file it automatically updates the clientside?
That's a pretty awesome feature, and I love sails but that feature is pretty cool.
Nodemon is a helpful development tool that watches the files in the directory that it was started in, and if anything changes are detected, your node.js application will automatically restart.
To install nodemon (you may need to use sudo)
$ npm install -g nodemon
Sails.js continually writes to the .tmp folder, and as a result you will find that nodemon will continually restart the server. To resolve this issue, simply ignore this folder by creating a .nodemonignore file with this single line, noting you can place any other files/folders you wish to ignore on separate lines
.tmp/*
To run your Sails.js application through nodemon
$ nodemon app
For more information, be sure to check out nodemon on npmjs.org
If you monitor nodemon --ignore 'tmp/*' --ext js,ejs . you will still get the infinite reload problem. Apparently Sails is constantly writing the ejs files.

Resources