Error: spawn convert ENOENT while using request npm package - node.js

My aim to obtain the buffer of the image which is stored in the s3 bucket. I am using a npm package named request which does a simple http/https request to the url provided on the s3 bucket. When I am using the package and doing the https request on the image url provided to me by s3 bucket I am getting the following error
events.js:85
throw er; // Unhandled 'error' event
^
Error: spawn convert ENOENT
at exports._errnoException (util.js:746:11)
at Process.ChildProcess._handle.onexit (child_process.js:1053:32)
at child_process.js:1144:20
at process._tickDomainCallback (node.js:381:11)
I went through this question on stackOverflow but couldn't find the resolution for my problem. My Code is as follows:
request(url, function (err, response,res1) {
if (err) {
//error Handling;
} else {
buffer = res1;
//Some Logic
}
});
Can Somebody help me with issue?

Related

nodejs how to get mongodb backup using application

I am working on a project that requires MongoDb backup after a certain period of time. The project is being built over MongoDB, Express as backend technologies with nodejs.
app is running on vercel and localhost. i want to get the backup from the application only.
i want to get the db backup using the application daily (i know about cron but i am stuck in backup part).
how can i get the mongodb backup using the nodejs application ?
i tried using the following code to get the backup (on my local host which is a windows) but failed :
const { spawn } = require('child_process')
const child = spawn('mongodump', [
`--db=${DB_NAME}`,
`--archive=${ARCHIVE_PATH}`,
'--gzip'
])
the error:
node:events:491
throw er; // Unhandled 'error' event
^
Error: spawn mongodump ENOENT
at Process.ChildProcess._handle.onexit (node:internal/child_process:283:19)
at onErrorNT (node:internal/child_process:478:16)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
Emitted 'error' event on ChildProcess instance at:
at Process.ChildProcess._handle.onexit (node:internal/child_process:289:12)
at onErrorNT (node:internal/child_process:478:16)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
errno: -4058,
code: 'ENOENT',
syscall: 'spawn mongodump',
path: 'mongodump',
spawnargs: [
'--db=store',
'--archive=C:\\tmp\\5484689.tar',
'--gzip'
]
}
i tried to solve the error but i couldnt and then now im trying to find a new way to get the mongodb backup on both localhost and vercel using the nodejs application.
and then i want to push the mongodb backup to minio s3.

Node.JS, node-imagemagick: Invalid paramaters/ENOENT on function call

I am writing Node.js script to create thumbnails with imagemagick. I running Windows 10. I have installed the latest Windows Binary release of ImageMagick, selecting the options to add it to the path and install legacy utilities (e.g., convert). I am using node-imagemagick to interface with ImageMagick from my script.
I can successfully use ImageMagick from the command line. For example, if I run the following command convert C:/Users/Admin/Desktop/in.jpg -resize 150x150 C:/Users/Admin/Desktop/out.jpg ImageMagick will create a thumbnail. However, if I run the convert function with the same arguments in my Node.js script:
var im = require('imagemagick');
im.convert(['C:/Users/Admin/Desktop/in.jpg', '-resize', '150x150', 'C:/Users/Admin/Desktop/out.jpg'], function (err, stdout) {
if (err) throw err;
console.log('stdout:', stdout);
});
Returns an error
Error: Command failed: Invalid Parameter - /Users
at ChildProcess.<anonymous> (C:\Users\Admin\Documents\Projects\node_script\node_modules\imagemagick\imagemagick.js:88:15)
at ChildProcess.emit (events.js:311:20)
at maybeClose (internal/child_process.js:1021:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5) {
timedOut: false,
killed: false,
code: 4,
signal: null
}
In case this was because of the path separators being different on Windows, I tried replacing the slashes with double backslashes and surrounding the paths in quotes ['"C:\\Users\\Admin\\Desktop\\in.jpg"',.... However, I instead get the error Error: Command failed: Invalid Parameter - -resize.
This also happens with the identify command. identify -format %[EXIF:*] C:/Users/Admin/Desktop/in.jpg sucussfully returns information about my image while my Node.js script:
im.identify(['identify', 'C:/Users/Admin/Desktop/in.jpg'], function(err, features){
if (err) throw err;
console.log(features);
});
Returns an error
Error: spawn identify ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:267:19)
at onErrorNT (internal/child_process.js:469:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
Emitted 'error' event on ChildProcess instance at:
at Process.ChildProcess._handle.onexit (internal/child_process.js:273:12)
at onErrorNT (internal/child_process.js:469:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'spawn identify',
path: 'identify',
spawnargs: [ 'identify', 'C:/Users/Admin/Desktop/in.jpg' ]
}
If these errors are not correctable or are unnecessarily hard to correct, is there a better way to create thumbnails in Node.js? I will eventually need to be able to generate thumbnails for videos and .swf files. The last publish of node-imagemagick was 7 years ago, is there anything more recent?
There is a much better alternative and actively maintained solution to node-imagemagick called sharp.
Usage (based on your question):
const sharp = require('sharp');
sharp('C:/Users/Admin/Desktop/in.jpg')
.resize(150, 150)
.toFile('C:/Users/Admin/Desktop/out.jpg')
.then(outputInfo => {
console.log(outputInfo);
})
.catch(reason => {
console.log(reason);
});
Found a comment saying to restart your device if using node imagemagick with Windows. After a restart commands called from the Node.js script worked without errors.

Unhandled 'error' event in NodeJs

I'm working in NodeJs and when trying to write file as .pdf with the help of 'fs' npm package I got some error and even pdf is not generating. I need help to jump out of this, issue is
events.js:167
throw er; // Unhandled 'error' event
^
Error: write EPIPE
at WriteWrap.afterWrite [as oncomplete] (net.js:833:14)
Emitted 'error' event at:
at onwriteError (_stream_writable.js:431:12)
at onwrite (_stream_writable.js:456:5)
at _destroy (internal/streams/destroy.js:40:7)
at Socket._destroy (net.js:603:3)
at Socket.destroy (internal/streams/destroy.js:32:8)
at WriteWrap.afterWrite [as oncomplete] (net.js:835:10)
Code regarding this error:
let doc = await wkhtmltopdf(options, xxxx);
let file = await doc.stdout.pipe(fs.createWriteStream(`${__dirname}/${id}_init.pdf`));
Thanks in anticipation!!
I have tried re-installing fs package, changing the ports but no improvement
I think the issue may be bcz of wkhtmltopdf package too(I'm using Mac)
I suggest you make the following changes, this is working very nicely for me:
// Set url and options to whatever you wish
let doc = wkhtmltopdf(url, options);
let file = doc.pipe(fs.createWriteStream(`${__dirname}/${id}_init.pdf`));
We can't use await on either call, since neither wkhtmltopdf nor doc.pipe return promises.
You can also try writing directly to file, skipping the stream, e.g.
wkhtmltopdf('http://google.com/', { output: 'out.pdf' });

AspNetCore NodeServices throws NodeInvocationException

I am using NodeServices to create PDF in AspNetCore app. App works fine on local machine but when deployed on production, on calling the pdf function, I get the following error:
Error: spawn D:\home\site\wwwroot\node_modules\phantomjs\lib\phantom\bin\phantomjs ENOENT
at _errnoException (util.js:1022:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:190:19) at onErrorNT (internal/child_process.js:372:16)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
Microsoft.AspNetCore.NodeServices.HostingModels.NodeInvocationException: Error during rendering report: spawn D:\home\site\wwwroot\node_modules\phantomjs\lib\phantom\bin\phantomjs ENOENT
Error: spawn D:\home\site\wwwroot\node_modules\phantomjs\lib\phantom\bin\phantomjs ENOENT
at _errnoException (util.js:1022:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:190:19) at onErrorNT (internal/child_process.js:372:16)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
I verified that all the files are at their right place. node_modules are present in the deployment.
Node Version on Azure: 8.9.4
Code to generate PDF:
module.exports = function (callback, html) {
var jsreport = require('jsreport-core')();
jsreport.init().then(function () {
return jsreport.render({
template: {
content: html,
engine: 'jsrender',
recipe: 'phantom-pdf'
}
}).then(function (resp) {
callback(/* error */ null, resp.content.toJSON().data);
}).catch(function (e) {
callback(/* error */ e, null);
});
}).catch(function (e) {
callback(/* error */ e, null);
});
};
If you use Azure App Service with a free plan, you cannot render pdf on the server. This is because the service runs in a sandbox and thus cannot run third-party executables, like node.exe. You need to upgrade your service's plan to 'Basic' or higher.
The issue was with the installation of Node packages. I was using Hosted Linux on Azure to build the solution. Switched to Hosted VS2017 build and everything is working smoothly.
https://github.com/pofider/phantom-html-to-pdf/issues/68

Where is child_process.js?

I am trying to debug a problem with forever/nodejs, and I get this stacktrace in the error log:
chdir(): Permission denied
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn EACCES
at errnoException (child_process.js:980:11)
at Process.ChildProcess._handle.onexit (child_process.js:771:34)
This looks to me like I should be able to look at line 980 of a file named "child_process.js" to see where the error is thrown. However, I cannot locate such a file on the system even with "find". Am I missing it somehow, or is the stracktrace lying?
It's part of the node.js source. See https://github.com/joyent/node/blob/master/lib/child_process.js. I believe you can also step directly through core node code using node's built-in debugger, or a package such as node-inspector.

Resources