how can I debug a node app that is started via the command line (cli) like forever or supervisor? - node.js

I'm familiar with debugging my own node apps (usually with node-inspector). Today I'd like to debug someone else's program. I'm trying to track down an issue with supervisor. So naturally I just add a --debug (or debug-brk) to the command call, but it passes that to the code that it is supervising.
I've tried adding debugger lines to the js file for supervisor but that didn't work (probably because no debugger was attached at that time). There's a bit of a race here -- I need to start the debugger and attach it to the supervisor process after it starts but before it processes its arguments from the command line.
What I really want to do here is stop supervisor and debug it before it processes its command line arguments. How can I do this?

I had the same problem while developing my hexo blog. The documentation isn't all that complete yet so I find myself needing to reverse engineer at times.
The basic idea is that in Node.js even your cli apps are simply normal node apps that you are exposing to the OS command line interface. On Unix systems you are using this line:
#!/usr/bin/env node
To allow the environment to execute the script.
Many cli based node apps try to insist that you install them globally with the -g option.
npm install -g node-inspector
I personally prefer to have as much control of my development environment as I can get, so I prefer to break some conventions and check my node_modules in to source control along with installing everything I can locally by dropping the -g.
npm install node-inspector
Now you don't have to do this in order to make this work, I'm just describing this setup because it relates to your question. When I run node-inspector I can't simply use:
node-inspector
Instead I must explicitly invoke it from within my project. I do this by executing the symlink in my node_modules/.bin folder:
node_modules/.bin/node-inspector
Now I'm running node-inspector just like you.
Next all I need to do is start the cli process in debug and optionally pass params to it:
node --debug-brk node_modules/.bin/hexo generate
Note I am explicitly calling the symlink here and not simply:
node --debug-brk hexo generate
If I tried the line above I would get an error: "Error: Cannot find module".
I hope this helps.

Related

Run React dev server as background process?

I've been recently experimenting with React and I want to be able to run the dev server that's initialized when you run npm start in the react app's directory as a background process on centOS so that I can continue to use the command line.
Is this possible with something like pm2? I tried various commands but couldn't get it to work, of course I can do the following "nohup npm start &" but I would much rather have all my managed processes using pm2 or some other process manager maybe?
Any advice?

Node.JS, CLI with VSCode not finding path to NPM modules, launch.json

I want to write a command line program to use via node.js and CLI module. VSCode is not finding .require('cli') so no intellisense.
Then when I launch for debug it isn't looking in the right place, it's looking at APP\npm_modules\cli.js when it is APP\npm_modules\cli\cli.js.
I do have cli installed both locally and globally and I have a NODE_PATH environment pointing accordingly.
The same code works correctly when run at the command line, so it's a VSCode issue. Perhaps, there is a different editor that offers debugging that I should consider?
My mistake. Turns out that I had enabled the debugger to break on all exceptions and as .require resolves, it is looking in several places, eventually the module is found, but there are a number of caught exceptions along the way. I was stuck at looking at the first exception.

Running hapijs as deamon

How can I run hapijs as a server deamon on a Linux box? Right now, I'm running it as a user process for development with the node index.js command for the main page, but in the long run it should be www-data or whatever else user that runs the process.
If you want to run node as a daemon without any extra tools, you can use nohup:
nohup node index.js &
However, the following tools can do this and also have some other really useful features such as automatic restart on exit, log redirection and in the case of PM2, clustering:
PM2: https://github.com/Unitech/pm2
Forever: https://github.com/foreverjs/forever
If you want your service to start when your machine start/reboots, you can use something like Upstart (on ubuntu) or System-V:
https://www.digitalocean.com/community/tutorials/how-to-write-a-linux-daemon-with-node-js-on-a-vps
To run as different user to the user you're logged in with:
sudo -u somebody node index.js
Please note that none of the above is specific to hapi but rather applies to any Node.js app.
PM2 is the best option hands down. It scales from local development through to production without issue.
First Step:
npm install -g pm2
The -g flag is simply for installing to globally so it's available as system command.
Second Step:
pm2 start index.js
The start command simply replaces node index.js Behind the scenes it runs the node process but as a daemon.
PM2 Actual Use Case
cd projects/my-app
npm install -g pm2
npm install
NODE_ENV=development pm2 start index.js -n my-app
pm2 stop my-app
pm2 restart my-app
pm2 status
pm2 logs my-app
pm2 m
Those should be enough to get you going. The nice thing about PM2 is it works great in a CI/CD environment as well since you can recall process by name. Finally out of the box it does log rotation and a few other awesome things to keep you going even if stuff goes south. Apps will also auto restart if they crash (obv. configurable).
Additional configuration allows PM2 to watch files on disk and restart the app as they change. This is great for development as you can code + save files and the API you're building in HapiJS will simply restart and your changes are live.
I use supervisord and it works great.
In short you have to configure supervisord to start your hapijs application. In addition you need to configure nginx or apache to reverse proxy requests to your hapijs application.
You can find detailed instructions on set up at http://blog.risingstack.com/operating-node-in-production/
It feels odd to suggest a tool when you haven't explicitly asked for one. nohup-ing the process and running in the background is an option that requires no new tooling, but for what it's worth, I would suggest Docker-izing the application and letting docker handle everything. Docker has several features built in, and even though it is not just for creating a daemon (it does so much more), you can use it's restart='always' feature to keep a process running.
Hope that helps.

Issue with Node-based Cordova Hook

I'm playing around with the Cordova hooks capabilities and I'm trying to test using a node application as a hook. In this article: http://devgirl.org/2013/11/12/three-hooks-your-cordovaphonegap-project-needs/ it references running node applications, so I know it's possible.
I've created a simple node application that I'm using to test the before prepare and after prepare process:
#! /usr/bin/env node
console.log("this is a node module");
When I run my prepare, I get the following error:
C:\Users\jwargo\dev\lunchmenu>cordova prepare
The system cannot find the path specified.
Hook failed with error code 1: C:\Users\jwargo\dev\lunchmenu\hooks\before_prepare\test.js
I can't find any information anywhere about what an error code of 1 means here.
I've tested the node code and it runs fine with "node test.js" and when I execute test.js from the command line Windows simply launches my default editor.
So, can anyone tell me what I'm doing wrong or what I need to do to be able to execute a node application as a hook with the Cordova CLI?
Figured it out with some help from the Cordova dev team. The space in my shebang was causing the problem. I removed it and the problem went away.

does node have a place to put custom console methods like ~/.irbrc?

I was thinking it would be handy if I could reload! code in the Node console, something to the effect of:
reload('./path/to/my/file.js')
that would delete the code from the cache then load it again -- handy for exercising prototype code. Seemed like a natural function to place in node's equivalent of ~/.irbrc. But I can't find that. Does node have such a thing?
I don't know there's such a tool. But I believe what you need can be achieved by using nodemon.
Use sudo npm install nodemon -g to install it. Then launch your application by nodemon app.js instead of node app.js. Then it watches for changes to nearby .js files, and automatically restart when you save.

Resources