Node js : spawn throws error in linux - javascript

The following is my code, It works in windows with out error but when I setup my project in server (linux) it is not working and throws error
var spawn = require('child_process').spawn,
javaCmd= spawn('java', ['-cp',__dirname+'/Java/jdk1.7.0_45/lib/dom4j.jar;'+__dirname+'/Java/jdk1.7.0_45/lib/geronimo-stax-api_1.0_spec-1.0.1.jar;'+__dirname+'/Java/jdk1.7.0_45/lib/gson-2.2.4.jar;'+__dirname+'/Java/jdk1.7.0_45/lib/mysql-connector-java-5.1.6.jar;'+__dirname+'/Java/jdk1.7.0_45/lib/ooxml-schemas-1.0.jar;'+__dirname+'/Java/jdk1.7.0_45/lib/poi-3.9-20121203.jar;'+__dirname+'/Java/jdk1.7.0_45/lib/poi-ooxml-3.9.jar;'+__dirname+'/Java/jdk1.7.0_45/lib/xmlbeans-2.5.0.jar;'+__dirname+'/Java/jdk1.7.0_45/lib/xmlbeans-xmlpublic-2.6.0.jar;'+__dirname+'/Java/jdk1.7.0_45/lib/excelreader.jar', 'astral.excelreader.Main', catid, id,target_path]);
javaCmd.stdout.on('data', function (data) {
console.log(data);
});
javaCmd.stdout.on('close', function(code) {
console.log(code);
});
javaCmd.stderr.on('data', function (data) {
console.log(data);
});
Following is the error
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn ENOENT
at errnoException (child_process.js:980:11)
at Process.ChildProcess._handle.onexit (child_process.js:771:34)
Anybody know its reason? I did n't set any other path for java. I tried to set using $ vi ~/.bash_profile but I got the following response
-bash: $: command not found
Anybody know how to set path in linux server

Related

Catch error on IBM Watson IoT NodeJS client

I'm using the IBM Watson IoT NodeJS client to connect and use IBM Watson IoT.
This works when my object with credentials etc. is correct:
var client = new ibm_watson_iot.IotfGateway(MY-JSON-OBJECT-WITH-CREDENTIALS);
But if credentials is wrong, then I get:
events.js:160
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND 1234xyz.messaging.internetofthings.ibmcloud.com 1234xyz.messaging.internetofthings.ibmcloud.com:8883
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
error: Forever detected script exited with code: 1
How do I correctly catch this error in a nice way?
You can always use try/catch block to handler error like that
try{
var client = new ibm_watson_iot.IotfGateway(MY-JSON-OBJECT-WITH-CREDENTIALS);
}
catch(error) {
console.log("Error in connection.. Probably configuration object")
}

spawn ls ENOENT when watched file is modified

I am learning Node.js via the book Node.js the Right Way. I am trying to run the following example to watch changes to a file called target.txt that resides in the the same directory as the .js file.
"use strict";
const
fs = require('fs'),
spawn = require('child_process').spawn,
filename = process.argv[2];
if (!filename) {
throw Error("A file to watch must be specified!");
}
fs.watch(filename, function () {
let ls = spawn('ls', ['-lh', filename]);
ls.stdout.pipe(process.stdout);
});
console.log("Now watching " + filename + " for changes...");
I get the following error when I change the text file and save it:
events.js:160
throw er; // Unhandled 'error' event
^
Error: spawn ls ENOENT
at exports._errnoException (util.js:1018: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)
Node.js version: v6.11.0
IDE: Visual Studio Code 1.13.1
OS: Windows 10 64x
There's no ls on Windows, you should use dir instead.
However, that's not an executable. To run .bat and .cmd files you can:
Spawn cmd.exe and pass those files as arguments:
require('child_process').spawn('cmd', ['/c', 'dir']);
Use spawn with the shell option set to true:
require('child_process').spawn('dir', [], { shell: true });
Use exec instead of spawn:
require('child_process').exec('dir', (err, stdout, stderr) => { ... });
For more on that, take a look at this section in the official docs.
EDIT:
I'm not sure I understood you question in the comment correctly, but if you go for the second option, for example, you code will look like this:
...
fs.watch(filename, function () {
let dir = spawn('dir', [filename], { shell: true });
dir.stdout.pipe(process.stdout);
});
...
Please, keep in mind you may need to adjust this code slightly. I'm writing all this from memory as I don't have access to a Windows machine right now, so I can't test it myself.

Videoshow NPM module with ffmpeg

I'm trying to use videoshow to convert multiple images to video, I have tried to change my code multiple times but now as you can see this is basically the same as in the module's description page. Still I am receiving the following error as stated below.
videoshow(images)
.save('video.mp4')
.on('start', function (command) {
console.log('ffmpeg process started:', command)
})
.on('error', function (err) {
console.error('Error:', err)
})
.on('end', function (output) {
console.log('Video created in:', output)
})
Error: Error: ffmpeg exited with code 1: Error reinitializing filters!
Failed to inject frame into filter network: Invalid argument
Error while processing the decoded data for stream #2:0
Conversion failed!
at ChildProcess.<anonymous> (C:\Users\xxxxxx\Documents\Visual Studio 2017\Projects\Frame\Frame\server-side\node_modules\fluent-ffmpeg\lib\processor.js:182:22)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at Process.ChildProcess._handle.onexit (internal/child_process.js:215:12)
Check aspect ratio and resolution of images, if your all images or any one have different resolution and aspect ratio then it will give you error. try with all same resolution images.

Node.js Unhandled Error

I am having an issue with my Node.js script in the Cloud9 online IDE. I am using the child_process module to run a python script, pass an input with stdin, and receive an output with stdout. I keep receiving an error. Here is the part of my code in question
Node.js
try {
var spawn = require ("child_process").spawn;
var pyProcess = spawn ("python2", ["pythonScript.py"]);
pyProcess.stdin.write ("Test");
pyProcess.stdout.on ("data", function (data) {
console.log (data);
});
} catch (e) {
console.log (e);
}
Python
import sys
sys.stdout.write (sys.stdin.read())
sys.stdout.flush()
Error
<Buffer 68 65 6c 6c 6f>
events.js:141
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at exports._errnoException (util.js:907:11)
at Pipe.onread (net.js:557:26)
When I remove the line
pyProcess.stdin.write ("Test");
the error goes away. Any and all help would be appreciated.

Node.js - simple example and EPERM error

var server = require('http').createServer(function(req, res){
});
server.listen(8080);
This simple example return error in console:
node node_server.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: listen EPERM
at errnoException (net.js:670:11)
at Array.0 (net.js:756:28)
at EventEmitter._tickCallback (node.js:192:41)
What is going on?
Port was locked. Topic shutdown.

Categories