Node js + imagemagick + Error: spawn ENOENT - node.js

Hi am trying to implement Imagemagick package using node.js
I added my following code.
var im = require('imagemagick');
router.post('/conversation/start', function(req, res) {
var args = ["/public/images/team/rajini.jpg", "-crop", "120x80+30+15", "output.png"];
im.convert(args, function(err) {
if (err) {
throw err;
}
res.end("Image crop complete");
});
});
But i getting the following error in terminal.
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn ENOENT
at errnoException (child_process.js:988:11)
at Process.ChildProcess._handle.onexit (child_process.js:779:34)
npm ERR! weird error 8
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian

Related

How to resolve setHeader issue in nodejs

I have done file uploading process in my application using connect-multiparty.I can upload file and move to local folder.Everything is working perfect except below issue.I do not know why i am getting this error.How to resolve this issue?How to avoid this error.
app.js:
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
var fs = require('fs');
app.post('/api/upload', multipartMiddleware, (req, res) => {
res.json({ 'message': req.files });
var tmp_path = req.files.uploads[0].path;
var target_path = './uploads/' + req.files.uploads[0].name;
fs.rename(tmp_path, target_path, function(err) {
if (err) throw err;
console.log("Upload started")
fs.unlink(tmp_path, function() {
if (err) throw err;
res.send('File uploaded to: ' + target_path);
});
});
res.end();
});
upload.component.ts:
upload(){
let formData = new FormData();
for(var i = 0; i < this.uploadedFiles.length; i++) {
formData.append("uploads[]", this.uploadedFiles[i], this.uploadedFiles[i].name);
}
this.http.post(environment.apiBaseUrl+'/upload', formData)
.subscribe((response)=>{
console.log('response receved is ', response);
})
}
Error:
_http_outgoing.js:526
throw new ERR_HTTP_HEADERS_SENT('set');
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:526:11)
at ServerResponse.header (C:\xampp\htdocs\testing\server\node_modules\express\lib\response.js:767:10)
at ServerResponse.send (C:\xampp\htdocs\testing\server\node_modules\express\lib\response.js:170:12)
at C:\xampp\htdocs\testing\server\app.js:65:17
at FSReqCallback.oncomplete (fs.js:154:23) {
code: 'ERR_HTTP_HEADERS_SENT'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! project#1.0.0 dev: `node app.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the project#1.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\ADMIN\AppData\Roaming\npm-cache\_logs\2020-05-21T17_06_06_701Z-debug.log

Node.js ENOENT Reading PDF file

I need to read pdf file and I use pdf-text-extract. It works perfectly on my localhost. But when I tried to run the program on server, I got the following error
spawn called
{ '0': 'pdftotext',
'1':
[ '-layout',
'-enc',
'UTF-8',
'/tmp/the_file_name.pdf',
'-' ],
'2': { encoding: 'UTF-8', layout: 'layout', splitPages: true } }
events.js:72
throw er; // Unhandled 'error' event
Error: spawn ENOENT
at errnoException (child_process.js:1011:11)
at Process.ChildProcess._handle.onexit (child_process.js:802:34)
Here is how I use pdf-text-extract
var extract = require('pdf-text-extract');
.....
.then (function () {
console.log(fs.readdirSync('/tmp'));
var extractAsync = Promise.promisify(extract);
return extractAsync(filePath);
})
.catch (function (err) {
console.log(err);
});
As you can see, I have added catch, but why the error is Unhandled 'error' event.
I have also checked that the file is exist using fs.readdirSync. What cause the error and how can I fix it?
Your server does not have the pdftotext command, which the pdf-text-extract module tries to spawn as a child process. The readme for the module includes a link to how to install the program for various platforms.

Node.js throw new Error('Can\'t set headers after they are sent.');

This the error:
{}
_http_outgoing.js:335
throw new Error('Can\'t set headers after they are sent.'); Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
at ServerResponse.setWriteHeadHeaders (D:\xampp\htdocs\wisaa\node_express\node_modules\morgan\node_modules\on-headers\index.js:80:19)
at ServerResponse.writeHead (D:\xampp\htdocs\wisaa\node_express\node_modules\morgan\node_modules\on-headers\index.js:39:36)
at ServerResponse.writeHeader (_http_server.js:233:18)
at Object.queue.drain (D:\xampp\htdocs\wisaa\node_express\routes\index.js:52:13)
at D:\xampp\htdocs\wisaa\node_express\node_modules\async\lib\async.js:871:23
at D:\xampp\htdocs\wisaa\node_express\node_modules\async\lib\async.js:44:16
at D:\xampp\htdocs\wisaa\node_express\routes\index.js:43:21
at Socket.<anonymous> (D:\xampp\htdocs\wisaa\node_express\node_modules\email-verify\index.js:145:13)
at Socket.emit (events.js:129:20)
npm ERR! Windows_NT 6.2.9200 npm ERR! argv "C:\\Program Files (x86)\\nodejs\\\\node.exe" "C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start" npm ERR! node v0.12.5 npm ERR! npm v2.11.2 npm ERR! code ELIFECYCLE npm ERR! node_express#0.0.0 start: `node ./bin/www` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the node_express#0.0.0 start script 'node ./bin/www'. npm ERR! This is most likely a problem with the node_express package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! node ./bin/www npm ERR! You can get their info via: npm ERR! npm owner ls node_express npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request: npm ERR! D:\xampp\htdocs\wisaa\node_express\npm-debug.log`
This is my code:
var express = require('express');
var router = express.Router();
var _ = require('underscore');
var fs = require('fs'),
async = require('async'),
verifier = require('email-verify');
/* GET home page. */
router.post('/', function(req, res, next) {
var emails = req.body.emails;
emails = emails.split(',');
var queue = async.queue(function(task, callback) {
task(callback);
}, 100);
_.each(emails, function(e, i) {
queue.push(function(cb) {
var done = false;
verifier.verify(e, function(err, info) {
if (err) {
//console.log(err);
// fauile
emails.splice(emails.indexOf(e), 1);
var score = "Your email is nor valid neither exist";
} else {
if (info.success) {
// success
res.json({
'message': emails
});
} else {
emails.splice(emails.indexOf(e), 1);
}
}
if (!done) {
done = true;
cb(null, true);
console.log('something bad happened!');
}
});
});
});
queue.drain = function() {
console.log(arguments);
res.writeHeader(200, {
'content-Type': 'text/plain'
});
res.send(JSON.stringify(emails));
res.end();
}
});
module.exports = router;
It looks like you'll be calling res.json() one or more times per request. That's not allowed because it sends the response (and headers hence error). There should be only one response per request.
When you do _.each, you're making a loop. In this case, you're adding a bunch of functions to a queue. When each function in that queue is processed you're calling res.json(). res.json() sends a response which is something you can only do once, not multiple times.
You can do any kind of processing of the emails that you need in this queue if you need to but don't actually send it with res.json until the callback is called, so in drain.
As already pointed out by #Matt Harrison , i am trying to add little description to it only.
It looks like,
res.json({
'message':emails
});
is getting called first which is responsible for sending the json response to the client, hence completing the request-response cycle.
So, after this point any attempt on res object is immaterial as response has already been sent.
Since you are getting following exception
new Error('Can\'t set headers after they are sent.');
So, most probably, queue.drain() is called, which is setting the header. At this point you are getting the error.
try:
verifier.verify(e, function(err, info) {
if (err) {
//Return the error itself
return res.send(err);
//The rest of your code here
...
}else{...}
you need to add the 'return' so that you don't reply twice. In a nutshell, Your code needs to have a return or an else so when there's an error you don't execute both code paths. That's where the error is coming from in my opinion.
Hope this helps.!

resize picture with nodeJs imageMagick

I would like to resize some picture before send to user in nodeJs with express
im = require("imagemagick")
app.get('/image/:dossier/:id/:taille', function (req, res) {
var image = __dirname + '/public/img/bibliotheque/'+req.params.dossier+'/'+req.params.id+'.jpg';
im.resize({
srcPath : image,
width : req.params.taille
},
function(err, stdout, stderr) {
if (err){
log.error(err);
} else {
res.contentType("image/jpeg");
res.end(stdout);
}
});
});
but it's return :
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)
I try to launch my app with and without sudo but no change
I'm on OSX
Please, help me
You need to install the application in addition to the npm module.
brew install imagemagick

Forever-monitor throwing ENOENT and not working

so I have:
var forever = require('forever-monitor');
var Monitor = forever.Monitor;
var child = new Monitor('clusters.js', {
max: 10,
silent: false,
killTree: true,
logFile: './logs/forever.log',
outFile: './logs/app.log',
errFile: './logs/error.log'
});
child.on('exit', function (err) {
console.log('Server exitted');
});
child.start();
and it always throw the same error: events.js:72 throw er; // Unhandled 'error' event with:
Error: spawn ENOENT
at errnoException (child_process.js:980:11)
at Process.ChildProcess._handle.onexit (child_process.js:771:34)
npm ERR! weird error 8
npm ERR! not ok code 0
Does anyone know what is going on and how to fix it?
Im on Windows 7 with:
"express": "3.3.5",
"forever-monitor": "~1.2.2"
https://github.com/blai/grunt-express/issues/12
Apparently the problem is with forever-monitor 1.2, I downgraded to 1.1 and it just worked.
From what I got there they dont seem to be doing anything about it either...

Resources