sails debug command not working in Sails.js - javascript

I am creating my first sails.js app. When I tried
sails debug
I'm getting the following error on my command prompt
Debugger listening on port 5858
info: Starting app...
error: Grunt :: Error: listen EADDRINUSE
at exports._errnoException (util.js:746:11)
at Agent.Server._listen2 (net.js:1129:14)
at listen (net.js:1155:10)
at Agent.Server.listen (net.js:1240:5)
at Object.start (_debugger_agent.js:20:9)
at startup (node.js:86:9)
at node.js:814:3
To get the PID of the process using port:5858, I tried running
C:\Windows\system32>netstat -a -n -o
but unfortunately there is no process bound to port 5858. Am I missing something here?
I'm using Windows 8.1 with node.js v0.12.0 and sails.js 0.11.0

My server uses node 0.10.38 with sails because of some weird unfixed grunt thing with 11+. Haven't pulled up this issue in a while, but it looks like there's new activity... check out this comment in particular, which explains the issue and a possible fix (direct quote):
Possible Solution:
Looking at the options for child_process.fork, the --debug flag is being passed down to the child upon exiting the womb i.e. running
sails debug :
// ./node_modules/sails/bin/sails-debug.js
// Spin up child process for Sails
Womb.spawn('node', ['--debug', pathToSails, 'lift'], {
stdio: 'inherit'
});
setting options.execArgv to an empty array removes the flag and allows the process to continue:
// ./node_modules/sails/lib/hooks/grunt/index.js
var child = ChildProcess.fork(
path.join(__dirname, 'grunt-wrapper.js'),
[
taskName,
'--pathToSails='+pathToSails,
'--gdsrc='+ pathToSails + '/node_modules'
],
{
silent: true,
stdio: 'pipe',
execArgv: []
}
);

It seems like a bug in Sails. You can apply the fix your self by replacing your Sails' file:
./node_modules/sails/lib/hooks/grunt/index.js
with the contents of the following:
https://raw.githubusercontent.com/balderdashy/sails/88ffc0ed9949f8c74ea390efb5610b0e378fa02c/lib/hooks/grunt/index.js
This is the file that will be in the Sails' release v12.

Did you try to run in debug like simple node.js?
node --debug app.js

Related

Why do I get "node: bad option" error when I pass a custom flag to nodejs command line, in a Angular 9 project

I am trying to add a custom flag in the npm start command, in an Angular 9 project, so that inside the local proxy server, I can intercept the proxy request and manipulate data. But I keep getting the "node: bad option" error. Cannot figure out why. Please help me. Below is my npm start command
"start" : "node -max_old_space_size=8192 --enable-mock ng serve --proxy-config proxy.config.js --host=0.0.0.0 --live-reload false";
then I just run npm start, then the "node: bad option: --enable-mock" error shows up in the console, I cannot even start the development server.
When passing args to a program, Node is strict:
No args: node [NODE_OPTIONS] [FILE]
Args: node [NODE_OPTIONS] <FILE> [PROGRAM_OPTIONS]
See? When passing args, you need to include a file (use . in this case for the project index.js.)

How to automate the build from the following configuration using gulp

Backdrop
I have a loopback and Angular app, Loopback gives use the server models and api's and using its sdk we are able to get client services.
Now i am planning to automate the following build process using gulp.
If any changes in the model is made then the sdk command is run and also the server is restarted/ and secondly when any changes to the angular files the sdk files are run and files are fetched from angular dist folder and server is restarted and best possible we can use live reload of browser.
Here is what i have tried and this never seems to work have been working on this for days.
Update
I was able to automate most of the stuff the one place where it fails is
gulp.task('browser-sync', function() {
browserSync.init(null, {
proxy: 'http://localhost:3000/home',
browser: 'google chrome',
port: 7000,
});
gulp.watch(['client/src/app/*.ts'], browserSync.reload);
let watcher = gulp.watch(['./common/models/**.js', './server/**.js', 'gulpfile.js'], ['sdk', 'server']);
watcher.on('change', function(event) {
console.log('File ' + event.path + ' was ' + event.type + ', running tasks...'); // this watcher
});
});
gulp.task('sdk', function() {
spawn('./node_modules/.bin/lb-sdk', ['server/server.js', './client/src/app/shared/sdk', '-q'], {stdio: 'inherit'});
});
This watcher restarts the server and runs the sdk but it is failing in the sdk
The stack trace please help
via remoting. The Angular code for this scope won't be generated.
[19:29:37] Starting 'sdk'...
[19:29:37] Finished 'sdk' after 11 ms
[19:29:37] Starting 'server'...
[19:29:37] Finished 'server' after 17 ms
events.js:163
throw er; // Unhandled 'error' event
^
Error: spawn ./node_modules/.bin/lb-sdk ENOENT
at exports._errnoException (util.js:1050:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
at onErrorNT (internal/child_process.js:367:16)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
Update
I have multiple gulp task and one such is ng build -w which happens in a new directory for the same i change the process.chdir to change path and i also keep tab of this sdk so do i need to check the path again her . How can i check or give absolute paths in my spawn . if this is one of the probable causes of failure
Taking into account your update
What might be happening is that once you change your directory using process.chdir for a sepearate task and also you have kept watch on all the tasks . The path is set to the previous path and the gulp task is not able to find the sdk i:e spawn('./node_modules/.bin/lb-sdk', ['server/server.js', './client/src/app/shared/sdk', '-q'], {stdio: 'inherit'}); in that respective path .
To fix this you can add the following check in the sdk task
gulp.task('sdk', function() {
if (process.cwd() != __dirname) { // this checks for the current path
process.chdir(<change path>); // if it dosent match your base path change it here
}
spawn('./node_modules/.bin/lb-sdk', ['server/server.js', './client/src/app/shared/sdk', '-q'], {stdio: 'inherit'});
});
Are you sure you've run npm install --save-dev #mean-expert/loopback-sdk-builder?
Are you sure that gulpfile.js is in the same dir as package.json?
Are you certain ./node_modules/.bin/lb-sdk exists?
Have you tried reinstalling everything?
The answer to the error is simply that your spawn function can't find ./node_modules/.bin/lb-sdk. This is either because the file doesn't exist, or because the it can't be found relatively to your gulpfile.js
The ENOENT error in the console means "error: no entity". It comes from UNIX, rather than Node itself, and basically just translates to "file not found", but applies to a variety of generic things, not just files/directories.
Check that the file ./node_modules/.bin/lb-sdk definitely exists. Then check that your gulpfile is in the root relative to that directory.

Node's spawn() silently failing when called from a forever script scheduled on boot

This is kind of a doozy. This issue is most likely server related and so my first recourse was AskUbuntu over here.
I'm trying to have crontab or rc.local or init.d to start a forever script on boot. It attaches a server to a port I can ping with some information and have it run a headless browser for me.
That said, it seems that I'm unable to get a response from Node.js's spawn():
var CASPER_PATH = '/home/ubuntu/dev/casperjs/bin/casperjs'; // actual binary location, not a symlink
var SCRIPTS_PATH = '/home/custom_user/endpoints/server.js';
var fileName = req.body.source + '_' + req.body.type + '.coffee'; // looks like: mysource_my_scrape_type.coffee
var scrapeId = 'test_scrape';
var user = 'user123';
var pass = 'pass123';
if (fs.existsSync(SCRIPTS_PATH + fileName)) {
// If file is in place, spawn casperjs
var sP = spawn(CASPER_PATH,
[SCRIPTS_PATH + fileName, '--ssl-protocol=any', '--user='+user, '--scrapeId='+scrapeId, '--pass='+pass],
{ detached: true },
function (err, stdout, stderr) {});
sP.stdout.on('data', function(data) { console.log('stdout', data.toString('utf8')); });
sP.stderr.on('data', function(data) { console.log('stderr', data.toString('utf8')); });
sP.stdout.on('close', function(code) { console.log('close', code); });
res.send({ scheduled: true, key: scrapeId });
} else {
res.send({ scheduled: false, error: 'Incorrect source, type or the script is missing.' });
}
Before I added the PHANTOMJS_EXECUTABLE env to crontab or rc.local (doesnt seem to matter no matter the user level), stdout was useful:
stdout Fatal: [Errno 2] No such file or directory; did you install
phantomjs?
close false
Now that the environment var is there, there is no output at all after spawn().
Mind you, Casper starts up just fine if a user (of any privilege level) runs node/forever from bash.
How can I see why spawn() is failing?
This actually looks like a combo-bug between forever, spawn and casperjs (maybe phantomjs).
I was able to reproduce your problem, here is the full code of my test application.
You didn't show the full code, so my guess is that you have an express application and there is a special URL to run the casperjs script.
I build a simple app like this and it behaved this way:
Just start app with node script.js (script.js is the express app which runs the casperjs script in server.js) - it works OK, renders response and writes output from the child process event handlers to console
Start app as root with init.d script - doesn't work, once the child is spawned, no event handlers are triggered
Start app as root with init.d script, replace casperjs with echo - the same, doesn't work (see, here we have this problem with just forever running as root, spawn and echo)
Start app as a regular user (not root) with init.d, replace casperjs with 'echo' - it works, event handlers are triggered, here I was almost sure the issue is solved, but ... :(
Start app as a regular user (not root) with init.d, put back casperjs - it doesn't work again, event handlers are not triggered
The practical solution to this it to use pm2, I did this:
# install pm2
sudo npm install -g pm2
# generate init.d scripts for pm2
# this command will fail, but hint about the correct format with sudo
pm2 startup ubuntu
# do this in the folder with your application
pm2 start script.js
# remember your application
pm2 save
# also useful
# sudo service stop/start/restart pm2
# pm2 stop/start/restart script
Now pm2 will start automatically with the system and it will launch your application. Everything works, child process event handlers are triggered.
I did not understand your requirement completely. But i do have a similar situation with Ubuntu headless server.
what i am trying to do here is what i did
First, How is my crontab ?
crontab -u USER -e
#reboot exec sudo -u USER /bin/bash /home/USER/SHELL_SCRIPT.sh
See, here i am actually starting a shell script, and not a node server
Now inside this shell script(SHELL_SCRIPT.sh)
#! /bin/bash
# SHELL_SCRIPT.sh
cd /home/USER/
/home/USER/.npm-packages/bin/forever start -p /home/USER -a -d --watch false --pidFile /home/USER/forever.pid -l /home/USER/forever.log -o /home/USER/forever.out -e /home/USER/forever.err /home/USER/MY_NODE.js
and even inside my MY_NODE.js i follow absolute path, i just ignore $PATH, and don't use that.
Inside this node server, I do 100's of spawn
Now, i did this around 2 years back, so if you ask me why do this way, which i cannot answer

How to restart node server on file change

I'm developing a node/express app
$> ./node_modules/babel/bin/babel-node.js index.js
Now I would like to reload the application if I make changes to index.js or any other dependency. How can I do this. I guess I have to use gulp for this, but than still I would like to have some advice on how to do this (which modules to use ect)
UPDATE: I've just tested with supervisor, but when something changes I get the following error:
$> /node_modules/.bin/supervisor --exec ./node_modules/babel/bin/babel-node.js index.js
crashing child
Starting child process with './node_modules/babel/bin/babel-node.js index.js'
events.js:85
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at exports._errnoException (util.js:746:11)
at Server._listen2 (net.js:1146:14)
at listen (net.js:1172:10)
at Server.listen (net.js:1257:5)
UPDATE: I just tried nodemon but I get the same errors as with supervisor:
$> nodemon --exec ./node_modules/babel/bin/babel-node.js index.js --watch libs
...
22 Aug 16:58:35 - [nodemon] restarting due to changes...
22 Aug 16:58:35 - [nodemon] starting `./node_modules/babel/bin/babel- node.js index.js`
events.js:85
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at exports._errnoException (util.js:746:11)
at Server._listen2 (net.js:1146:14)
at listen (net.js:1172:10)
UPDATE: I've solved the EADDRINUSE issue by adding the following to index.js
process.on('exit', () => {
server.close();
})
process.on('uncaughtException', () => {
server.close();
})
process.on('SIGTERM', () => {
server.close();
})
However, now it seems to restart, but the new code is not loaded
use this:
supervisor -- -r 'babel/register' index.js
and remove server.close code.
I was really disappointed with the performance of all the solutions where you run babel-node within nodemon (or supervisor). So I built this:
https://github.com/kmagiera/babel-watch
You can use it as follows (perhaps in your package.json scripts section):
babel-watch -w src src/main.js
The difference is that instead of restarting whole babel-node process on every file change (which takes like 1.5sec on my MBP) it runs babel in parent process and starts "pure" node process with all transpiled JS files provided at your script startup.
Use nodemon:
Install it globally:
npm install -g nodemon
Use it on your project:
nodemon myscript.js
It will watch for changes in your project directory and restart the script when it sees them.
There are a lot of tools to do this. Take a look at this post:
Restart node upon changing a file
Maby the most common is Supervisor:
https://github.com/petruisfan/node-supervisor
The most popular tools for that purpose are nodemon, forever and supervisor. You can install them via npm. For other tasks like css pre-processors, minification, tests run etc. You can use task managers like Grunt or Gulp
use this supervisor -- -r 'babel-register' index.js because Error: Cannot find module 'babel/re‌​gister'. after checked the modules ,i thought it changed to babel-register and it works for me

Is it necessary to use Forever.js on OpenShift?

I've deployed my first Node.js app on OpenShift's free tier, and it works great.
Will OpenShift automatically restart my Node app when it crashes, or do I have to set up Forever.js? I tried setting it up, and it would not work. After running node_modules/forever/bin/forever start app.js (working dir was app-root/repo, with local copy of forever) I got this output:
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: app.js
fs.js:240
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT, no such file or directory '/var/lib/openshift/5397416f5004466c0b000080/.forever/VQMF.log'
at Object.openSync (fs.js:240:18)
at Object.startDaemon (/var/lib/openshift/5397416f5004466c0b000080/app-root/runtime/repo/node_modules/forever/lib/forever.js:406:14)
at /var/lib/openshift/5397416f5004466c0b000080/app-root/runtime/repo/node_modules/forever/lib/forever/cli.js:258:13
at /var/lib/openshift/5397416f5004466c0b000080/app-root/runtime/repo/node_modules/forever/lib/forever/cli.js:145:5
at Object.oncomplete (/var/lib/openshift/5397416f5004466c0b000080/app-root/runtime/repo/node_modules/forever/lib/forever.js:358:11)
So, does OpenShift manage my app's health for me, or will I need to get Forever working? If so, any idea as to the error I got?
Yes, OpenShift does automatically restart your Node app when it crashes. OpenShift doesn't use forever.js but it uses node-supervisor. Your can test it by requiring something that doesn't exist. Fix it quick though because the log can grow fast restarting the app. Here is the log in nodejs.log on OpenShift which shows that it's running node-supervisor:
DEBUG: Running node-supervisor with
DEBUG: program 'server.js'
DEBUG: --watch '/var/lib/openshift/53a9e06ae0b8cde26300008e/app-root/data/.nodewatch'
DEBUG: --ignore 'undefined'
DEBUG: --extensions 'node|js|coffee'
DEBUG: --exec 'node'
DEBUG: Starting child process with 'node server.js'
DEBUG: Watching directory '/var/lib/openshift/53a9e06ae0b8cde26300008e/app-root/data/.nodewatch' for changes.
Currently, OpenShift's default behavior involves using supervisor to start, watch, and restart your nodejs applications.
Here is a quick outline of the various init options for nodejs:
If your app includes a valid package.json file with a main entry (containing the name of your server script), then OpenShift will initialize your app by using supervisor to start that script.
If your app includes the force_npm_deploy marker file (an empty file in .openshift/markers/use_npm) - then OpenShift will just run npm start. This runs whatever is defined in your package.json file's scripts.start entity.
If all else fails, OpenShift will try to run server.js using supervisor (as a fallback option).
Some additional notes are available here: https://www.openshift.com/blogs/10-reasons-openshift-is-the-best-place-to-host-your-nodejs-app#npm

Categories