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.
Related
The gulp task
/* Run the npm script npm run buildLsdk using gulp */
gulp.task('sdk', function() {
if (process.cwd() != basePath) {
process.chdir('..');
// console.log(process.cwd());
}
spawn('./node_modules/.bin/lb-sdk', ['server/server.js', './client/src/app/shared/sdk', '-q'], {stdio: 'inherit'});
});
I am getting the following stack trace but i cannot debug
Error: spawn ./node_modules/.bin/lb-sdk ENOENT
at exports._errnoException (util.js:1022:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
at onErrorNT (internal/child_process.js:359:16)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
at Module.runMain (module.js:607:11)
at run (bootstrap_node.js:420:7)
at startup (bootstrap_node.js:139:9)
at bootstrap_node.js:535:3
I have all the necessary files in node modules too any help is really appreciated.
More reference on the File use above - https://github.com/rahulrsingh09/loopback-Angular-Starter/blob/master/gulpfile.js
I think it's because lb-sdk.cmd is the file you're supposed to run on windows. when I changed the command to the below the error goes away. Please note the windows style directory slashes are different from linux.
gulp.task('sdk', function() {
spawn(
'.\\node_modules\\.bin\\lb-sdk.cmd',
[
'.\\server\\server.js',
'.\\client\\src\\app\\shared\\sdk',
'-q'
], {stdio: 'inherit'}
);
});
I found some more information,and I'm going to post a second answer I found (above one was accepted).
If you want to avoid changing directories in windows/linux you can use cross-spawn: https://www.npmjs.com/package/cross-spawn
win-spawn (from the chat dialog) is not maintained anymore per the github repo . If you're interested in using it make the following changes:
npm install cross-spawn
npm install spawn-sync
npm install strongloop (needs to be reinstalled per this link)
change 'google chrome' in browser-sync task to chrome.exe per this link:
run gulp
Please refer to this similar answer, this will solve your issue:
Convert the following npm script to gulp task
How to automate the build from the following configuration using gulp
And you can check the documentation of lb-sdk by typing ./node_modules/.bin/lb-sdk in your terminal.
Can you try using basePath when passing server/server.js and ./client/src/app/shared/sdk. Like for example:
spawn(
'./node_modules/.bin/lb-sdk',
[
basePath + '/server/server.js',
basePath + '/client/src/app/shared/sdk',
'-q'
], {stdio: 'inherit'}
);
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
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
I am working through the tutorial on the jspm.io site
https://github.com/jspm/jspm-cli/wiki/Getting-Started
All works fine until I get to item 3, where I try to execute
jspm install jquery
and I get this error message
warn Error on getOverride for jspm:github, retrying (2).
ReferenceError: ui is not defined
at c:\Projects\Project1\node_modules\jspm\node_modules\jspm-registry\registry.js:157:5
nodejs is v0.12.0
npm is 2.5.1
jspm is 0.14.0
and this is on Windows 8.1
Does anyone have any clue what is causing this?
This looks like it was because there was an error while jspm was trying to create the local registry clone. Ensure you have git installed as git on your machine. Otherwise it may be a permissions issue.
This was a logging bug though - have fixed it with an update to the registry, so that the error should be slightly more useful next time if you update jspm.
I was getting a similar error with jspm but my problem was actually in how nodejs child_process.exec was calling the git command.
child_process.exec was running
C:\Windows\system32\cmd.exe /s /c "git clone --depth=1 github.com/jspm/registry.git .
However cmd.exe was still auto running commands set in the registry first. In my case the command changing the working folder. So the cwd was being overridden.
Check your registry settings for:
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
If there is a command in there to set the drive of working folder it will cause the above error.
Also
With your working folder as c:\, try ruuning the following nodejs code:
var exec = require('child_process').exec;
exec('dir', { cwd: 'C:/windows/fonts' }, function(error, stdout, stderr) {
console.log('stdout: ' + stdout);
});
If it does not list the contents of the fonts folder then your problem is more likely with child_process.exec in node
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