Node.js - execFile throws spawn UNKNOWN - javascript

I am trying to spawn a service created with pyInstaller from an electron application. I am using the following code for that:
return new Promise((reject, resolve)=>{
var exec = require('child_process').execFile;
exec(path.join(install_path, 'myService.exe'), ['--startup=auto', 'install'], function(err, data) {
if(err) {
reject(err);
return;
}
console.log(data.toString());
exec(path.join(install_path, 'myService.exe'), ['start'], function(err, data){
if(err) {
reject(err);
return;
}
resolve(data.toString());
})
});
}
Unfortunately, this throws an
Uncaught Error: spawn UNKNOWN
on a testing system, which does not have node installed and is running Windows 10 x64. On my machine it is working fine.
Does anyone have tips how I could investigate this further? I am especially curious how this error is uncaught, because the callback functions obviously contain simple error handling.

Okay, after I built in better error handling thanks to Keiths help and rebuilt the project, the testers could not reproduce the issue anymore. I am still not sure if that actually fixed the problem or if the testers pulled an old version the last time.
Anyway, this is solved.

Related

Meteor - Uncaught Error: Cannot find module 'mongodb'

I'm trying to establish a connection between my Meteor application and my MongoDB Atlas database.
I have the following bit of JavaScript:
var MongoClient = require('mongodb').MongoClient, format = require('util').format;
MongoClient.connect('<MyMongoURL>', function (err, db) {
if (err) {
throw err;
} else {
console.log("successfully connected to the database");
db.collection('largeTreeMap', function(err, docs) {
// Check for error
if(err) return console.log(err);
// Walk through the cursor
docs.find().each(function(err, doc) {
// Check for error
if(err) return console.err;
// Log document
console.log(doc);
})
});
}
db.close(); });
I added this to a blank JS document called test.js and upon running
node test.js
In my command line it returned the success message and data:
So now that I know the connection can be established I added the code to my Meteor project. I created a basic button and onClick the connection to MongoDB should completed.
However, instead I receive the following console error:
I understand from reading various Stack questions that this is a result of not running npm install mongodb in the project directory. However, I have tried doing this and the terminal returns:
Does any body know why the MongoDB is failing to install and preventing me from connecting to MongoDB in my application?
Any help would be much appreciated,
Many thanks,
G
You're trying to connect to the Mongo instance from the client, which is probably not what you want.
The mongodb npm package supports only Node.js, not JavaScript in the browser, as you can see from this line in its package.json
"engines": {
"node": ">=0.10.3"
},
In the case that worked, you are running it with Node.
What you probably want to do is to set the MONGO_URL environment variable to the Mongo Atlas instance, and leave the implementation of connecting / updating to Meteor itself.

Heroku - Headless Chrome - Connection Refused

I am currently working with the: Heroku Build Pack for headless chrome.
https://github.com/heroku/heroku-buildpack-google-chrome/
I'm encountering this infuriating error where my node script (show below) cannot connect to the chrome instance. I get a pretty definitive error being:
{ Error: connect ECONNREFUSED 127.0.0.1:30555
at Object.exports._errnoException (util.js:1018:11)
at exports._exceptionWithHostPort (util.js:1041:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
code: ‘ECONNREFUSED’,
errno: ‘ECONNREFUSED’,
syscall: ‘connect’,
address: ‘127.0.0.1’,
port: 30555 }
My node super simple script:
CDP((client) => {
// extract domains
// const {Network, Page} = client;
const Network = client.Network
const Page = client.Page
// setup handlers
Network.requestWillBeSent((params) => {
console.log(params.request.url);
});
Page.loadEventFired(() => {
client.close();
});
// enable events then start!
Promise.all([
Network.enable(),
Page.enable()
]).then(() => {
return Page.navigate({url: 'https://www.something.com/'});
}).catch((err) => {
console.error(err);
client.close();
});
}).on('error', (err) => {
// cannot connect to the remote endpoint
console.error(err);
});
Has anyone had any luck getting this type of thing to work?
My Procfile looks like this to first start Chrome, then my Node.js server:
web: /app/.apt/usr/bin/google-chrome & node app/server.js
(Used in Scraping Service, a REST API for scraping dynamic websites. It uses headless Chrome and Cheerio.)
Alright I figured it out. When deploying to heroku, I was using two different Procs in the Procfile. One for web which was launching the Node script. And another for launching the headless chrome daemon.
On heroku, those two different procs don't even share the same dyno. Meaning they we're on totally separate "boxes" - at least in theory. This resulted in them having different ports set in the ENVs (not that it even mattered at that point - they might as well be in different continents)
Solution:
Have the node script start the actual headless chrome, then ultimately connect to that child process using the CDP interface.
Also - if you're here and also curious about documentation for the CDP interface for node - it doesnt exist at the moment. Your best option, which is actually pretty good, is: https://chromedevtools.github.io/debugger-protocol-viewer/
Happy hunting.
Edit:
Example of how we handled launching the chrome child process from the application source
const spawn = require('child_process').spawn
spawn('/path/to/chrome/binary',[{`--remote-debugging-port=${process.env.PORT}`]) // Set by heroku
.on('close', () => console.log('CHROME_PROCESS_CLOSE'))
.on('error', e => console.log('CHROME_PROCESS_ERROR', e))
.on('exit', (e, z, a) => console.log('CHROME_PROCESS_EXIT', e, z, a))
.on('data', () => {})

Node.js callback not executed when using VSCode debugger

I have an issue with the VSCode Node.js debugger.
I have the following code (it downloads an image an calculates its hash):
var request = require('request');
var crypto = require('crypto');
request({ uri : 'http://static.wixstatic.com/media/28f6fa_1519eb247c97446098566248a9f86441.jpg',
encoding: null,
timeout: 10000
}, function (err, res, body) {
if (err) {
return res.status(500).send(err);
}
if (res.statusCode !== 200) {
return res.status(500).send(buildResponse(500, "Image download returned status code " + res.statusCode));
}
console.log(crypto.createHmac('sha256', body).digest('hex'));
});
If I run node test.js, it prints the hash of the file perfectly.
If I run it using VSCode debug mode, it does not.
If I set a break point at line 4 (request({...), the debugger hits the break point.
If I set a break point at line 8 (if (err) { ...), the debugger does not hit the break point.
Am I doing something wrong or is this a bug?
$ node -v
v4.6.0
VSCode version: 1.6.1 Recovery Build
Just use the node-inspector it is for me the better way to debug code of backend
here is the link and there are complete guides to start witht that
https://www.npmjs.com/package/node-inspector
By the way is you want to debug some specific file use the node-debug
node-debug my_file_to_debug.js
I hope it help you.

Node.js spawn/exec/execFile/win-spawn/cross-spawn all throwing Error: spawn ENOENT

For context, routes\index.js:87 is around my exec (sync) or inside my exec (async).
I am getting this error with either spawn, exec, or execFile, using the libraries child_process, win-spawn, or cross-spawn.
I've tried running node, npm, grunt, ant (Apache), etc. -- which all work from command line with no problems -- with and without parameters, with and without options, sync and async, and I always get this exact error (same line and column).
I spent all day yesterday looking for solutions, found solutions on here, and on Github (node), tried everything I found, but nothing solved the problem.
I will provide any info you think will help to solve the problem. Thanks for your support.
-
Requested by Ben Fortune (have tried sync and async of all, only shown first example):
var exec = require('child_process').exec;
exec('node', function(err){
if(err) throw err;
});
-
var exec = require('child_process').exec;
var child = exec('node');
child.on('error', function(){ throw arguments['0']; });
-
var exec = require('child_process').execFile;
exec('/path/to/node', function(err){
if(err) throw err;
}
-
var spawn = require('child_process').spawn
spawn('node', function(err){
if(err) throw err;
}
-
var spawn = require('child_process').spawn
spawn('cmd', ['/s', '/c', '"C:\\mycmd.bat"'], {
windowsVerbatimArguments: true
});
I have tried all of these with the packages win-spawn and cross-spawn, with and without the '.exec' (etc.) affixes to the require. I have tried options like stdio: 'inherit'.
I am using Windows 8.1 64-bit, running command as Administrator.
-
Node js - Error: spawn ENOENT (Windows 8.1)
http://www.zescience.com/node-js-error-spawn-enoent-windows-8-1-167370
A couple of examples with the same error, except in my case, I am calling the exec function, not some 3rd party package.
I've also tried putting the exec elsewhere in code, location of the call is not the problem.
Possibly related: https://github.com/joyent/node/issues/2318
Where win-spawn and cross-spawn failed, spawn-cmd worked.
I have no idea why cross-spawn didn't work when spawn-cmd did, as they're pretty similar, but there you go. As to the original problem, I guess it's an issue with node.

node-phantom createPage() never calls callback

I used nodejs with node-phantom module for some time. It worked fine.
Now I try it on another machine and same code example don't work:
var Scan=function(request,response)
{
var parsedURL=url.parse(request.url,true);
if(parsedURL.query.site)
{
console.log("scanning "+parsedURL.query.site);
phantom.create(function(err,ph) {
console.log(err);
return ph.createPage(function(err,page) {
console.log(err);
return page.open(parsedURL.query.site, function(err,status) {
console.log("opened site? ", status);
if (status=="fail") {
response.writeHead(404, {'Content-Type': 'text/plain'});
response.end('URL not found');
return;
}
var filename="temp.jpg';
console.log(filename);
page.render(filename,function(err){
if (err) {
console.log(err);
return;
}
page.close(function(){
response.writeHead(404, {'Content-Type': 'text/plain'});
response.end('URL not found');
});
});
console.log("opened site? ", status);
if (status=="fail") {
response.writeHead(404, {'Content-Type': 'text/plain'});
response.end('URL not found');
return;
}
var filename="temp.jpg';
console.log(filename);
page.render(filename,function(err){
if (err) {
console.log(err);
return;
}
page.close(function(){
response.writeHead(404, {'Content-Type': 'text/plain'});
response.end('URL not found');
});
});
});
});
});
}
}
It never gets inside createPage() callback and it looks like lack of communication between nodejs and phantomjs.
nodejs version: 0.10.10
phantomjs version: 1.9.1
How can I check what wrong with it?
UPD: Installed new Debian distro and now it throws folowing warning in the console sometimes.
warn - client not handshaken client should reconnect
It looks like socket.io problem.
Well hard to diagnose exactly but I tried your code with the following fixes for the mismatched quotations and it seems to work fine on v0.10.6. These 2 lines that look like:
var filename="temp.jpg';
need fixing first.
My best guess is that you have a 32 bit versus 64 bit problem... seeing as you switch machines and it fails or works. So I simulated that by installing 32 bit node on a 64 bit system to show the troubleshooting procedure for that sort of thing:
First check the exit code:
[node#hip1 blah]$ phantomjs
[node#hip1 blah]$ $?
-bash: 127: command not found
follow all the symlinks and run the executable directly, for example on my system:
[node#hip1 blah]$ which phantomjs
~/node/bin/phantomjs
[node#hip1 blah]$ ls -l ~/node/bin/phantomjs
lrwxrwxrwx. 1 node node 43 Jun 16 20:06 /node/node/bin/phantomjs -> ../lib/node_modules/phantomjs/bin/phantomjs
[node#hip1 blah]$ ls -l /node/node/lib/node_modules/phantomjs/bin/phantomjs
-rwxr-xr-x. 1 node node 540 Jun 16 20:14 /node/node/lib/node_modules/phantomjs/bin/phantomjs
execute that...
[node#hip1 blah]$ /node/node/lib/node_modules/phantomjs/bin/phantomjs
/node/libs/node-v0.10.6-linux-x86/lib/node_modules/phantomjs/lib/phantom/bin/phantomjs: error while loading shared libraries: libfreetype.so.6: cannot open shared object file: No such file or directory
Ah, notice the .6 on the end of that library, it's that this is a 64 bit system but we have installed 32 bit node to avoid memory issues and have also noticed better performance with it. So an npm install phantomjs goes and gets the 32 bit version of that. So now I'd need the devel i686 versions of those libraries, which won't be installed if I don't specify - instead I'll get the x86_64 versions. So do a few of these:
yum install freetype-devel.i686 or on debian use apt-get install. You might also need libfontconfig.so.1, which is in fontconfig-devel.i686.
And finally!
phantomjs>
After that things should probably work.

Categories