I am building a command line package.
I am using the spawn method to execute cd command.
Here's the code:
const cdChild = spawn('cd', [projectName])
cdChild.stdout.on('data', (data) => {
console.log(data.toString())
})
cdChild.on('error', (err) => {
console.log(err)
})
It gives the following error:
Error: spawn cd ENOENT
at Process.ChildProcess._handle.onexit (node:internal/child_process:282:19)
at onErrorNT (node:internal/child_process:477:16)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
errno: -4058,
code: 'ENOENT',
syscall: 'spawn cd',
path: 'cd',
spawnargs: [ 'bruh' ]
}
OS: Windows 10 21H1
Node: v16.13.0
Related
My docker container is running on port 27018. When I run following command from terminal, it's working correctly -
docker exec mongodb mongodump -u=Abhishek -p=abhishek --authenticationDatabase=testdb
But, when I run the same command from node.js, it's not working. Please have a look into my below code and help me debugging 🙏🙏
import { spawn } from 'child_process';
const backupProcess = spawn('sudo docker exec mongodb mongodump', [
`-u=Abhishek`,
`-p=abhishek`,
`--authenticationDatabase=testdb`
]);
backupProcess.on('error', (err) => {
console.log('Error :: ', err);
});
backupProcess.on('exit', (code, signal) => {
if (code) {
console.log(MessageType.warn, '\n Backup process exited with code ', code);
} else if (signal) {
console.log(MessageType.error, '\n Backup process was killed with signal ', signal);
} else {
console.log(MessageType.success, '\n Successfully backed-up the database ✅ ');
}
});
It's giving me following error -
Error :: Error: spawn sudo docker exec mongodb mongodump ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:268:19)
at onErrorNT (internal/child_process.js:470:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'spawn sudo docker exec mongodb mongodump',
path: 'sudo docker exec mongodb mongodump',
spawnargs: [
'-u=Abhishek',
'-p=abhishek',
'--authenticationDatabase=testdb'
]
}
I'm using the imageMagick subclass of graphicsmagick to alter ~7k png images. The function below gets about 2/3 of the way through and fails (see log).
Seems to be an issue where a socket is failing. How do I debug this?
A thought I have is to use the -limit method, but no dice...
Thanks in advance :)
Code:
// changing an image's resolution is the same thing as changing its DPI
export default async function changeScreenshotResolution(dimension) {
new Promise(() => {
try {
const screenshots = fs.readdirSync(path.join(__dirname, `../output/screenshots/${dimension}`), function(error) {if (error) console.log(error)});
screenshots.map(screenshot => {
try {
let readStream = fs.createReadStream(path.join(__dirname, `../output/screenshots/${dimension}/${screenshot}`));
let writeStream = fs.createWriteStream(path.join(__dirname, `../output/screenshots/${dimension}1/${screenshot}`));
im(readStream, screenshot)
.limit('memory', '32MB') // i tried variations of this but it made no difference
.units('PixelsPerInch')
.density(300,300)
.stream()
.pipe(writeStream)
console.log(screenshot);
} catch (error) {
console.log(`Error changing screenshot resolution: ${error}`);
}
})
} catch (error) {
console.log(`changeScreenshotResolution error: ${error}`);
}
})
};
Error: spawn convert EAGAIN
at Process.ChildProcess._handle.onexit (internal/child_process.js:268:19)
at onErrorNT (internal/child_process.js:468:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: -35,
code: 'EAGAIN',
syscall: 'spawn convert',
path: 'convert',
spawnargs: [
'-density',
'300x300',
'/Users/daddy/Dev/scout/project-sunroof-scraping/output/screenshots/rect/6550 ARABIAN CIR.png',
'-units',
'PixelsPerInch',
'/Users/daddy/Dev/scout/project-sunroof-scraping/output/screenshots/rect1/6550 ARABIAN CIR.png'
]
}
events.js:292
throw er; // Unhandled 'error' event
^
Error: read ENOTCONN
at tryReadStart (net.js:571:20)
at Socket._read (net.js:582:5)
at Socket.Readable.read (_stream_readable.js:474:10)
at Socket.read (net.js:622:39)
at new Socket (net.js:374:12)
at Object.Socket (net.js:265:41)
at createSocket (internal/child_process.js:313:14)
at ChildProcess.spawn (internal/child_process.js:436:23)
at Object.spawn (child_process.js:548:9)
at spawn (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/cross-spawn/index.js:17:18)
at gm._spawn (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/gm/lib/command.js:224:14)
at /Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/gm/lib/command.js:101:12
at series (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/array-series/index.js:11:36)
at gm._preprocess (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/gm/lib/command.js:177:5)
at gm.write (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/gm/lib/command.js:99:10)
at /Users/daddy/Dev/scout/project-sunroof-scraping/modules/changeScreenshotDPI.js:18:26
Emitted 'error' event on Socket instance at:
at emitErrorNT (internal/streams/destroy.js:100:8)
at emitErrorCloseNT (internal/streams/destroy.js:68:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: -57,
code: 'ENOTCONN',
syscall: 'read'
}
I have an app on a heroku hobby web dyno running a fairly basic node api.
We have a series of pdf's that get attached to a pdf generated by puppeteer.
My code works when I run it locally (windows) but it also works when I deploy to a my production heroku instance.
Here's the problematic code:
try{
//If there are pdfs to attach to the end of the file, we do it here.
let fileList = [];
console.log('*');
for (const file of pdfFileNameList) {
fileList.push("./pdfs/" + file.pdf);
}
console.log('**');
//Spawn a child process to run pdftk to merge the pdfs.
let execer = require('child_process').spawn;
let child;
let args = ['-'].concat(fileList).concat(['cat', 'output', '-']);
console.log(args);
console.log('***');
child = execer('pdftk', args, {
encoding: 'binary',
stdio: ['pipe']
});
//Write the file PDF binary data to the child process' stdin.
//It will combine that with the list of pdfs specified in fileList.
console.log('****');
child.stdin.write(data);
console.log('*****');
child.stdin.end();
console.log('******');
let bufferArray = [];
//shove the data from the child process into my bufferArray as it comes in.
child.stdout.on('data', function (pdfdata) {
bufferArray.push(new Buffer(pdfdata, 'binary'));
});
console.log('*******');
//When the process is done, send the concatted pdf to the user.
child.on('close', function () {
res.header('Content-Type', "application/pdf");
res.send(Buffer.concat(bufferArray));
});
console.log('********');
} catch (err) {
console.error(err);
}
And here's the resulting set of errors:
2020-01-16T18:02:37.830775+00:00 app[web.1]: *
2020-01-16T18:02:38.356266+00:00 app[web.1]: **
2020-01-16T18:02:38.358310+00:00 app[web.1]: [ '-', '/app/pdfs/Example.pdf', 'cat', 'output', '-' ]
2020-01-16T18:02:38.358371+00:00 app[web.1]: ***
2020-01-16T18:02:38.373374+00:00 app[web.1]: ****
2020-01-16T18:02:38.375832+00:00 app[web.1]: Error: write EPIPE
2020-01-16T18:02:38.375836+00:00 app[web.1]: at afterWriteDispatched (internal/stream_base_commons.js:149:25)
2020-01-16T18:02:38.375838+00:00 app[web.1]: at writeGeneric (internal/stream_base_commons.js:140:3)
2020-01-16T18:02:38.375843+00:00 app[web.1]: at Socket._writeGeneric (net.js:776:11)
2020-01-16T18:02:38.375845+00:00 app[web.1]: at Socket._write (net.js:788:8)
2020-01-16T18:02:38.375847+00:00 app[web.1]: at doWrite (_stream_writable.js:435:12)
2020-01-16T18:02:38.375849+00:00 app[web.1]: at writeOrBuffer (_stream_writable.js:419:5)
2020-01-16T18:02:38.375851+00:00 app[web.1]: at Socket.Writable.write (_stream_writable.js:309:11)
2020-01-16T18:02:38.375853+00:00 app[web.1]: at Object.returnReport (/app/models/report_functions.js:245:33) {
2020-01-16T18:02:38.375855+00:00 app[web.1]: errno: 'EPIPE',
2020-01-16T18:02:38.375857+00:00 app[web.1]: code: 'EPIPE',
2020-01-16T18:02:38.375859+00:00 app[web.1]: syscall: 'write'
2020-01-16T18:02:38.375861+00:00 app[web.1]: }
2020-01-16T18:02:38.378481+00:00 app[web.1]: events.js:200
2020-01-16T18:02:38.378484+00:00 app[web.1]: throw er; // Unhandled 'error' event
2020-01-16T18:02:38.378486+00:00 app[web.1]: ^
2020-01-16T18:02:38.378488+00:00 app[web.1]:
2020-01-16T18:02:38.378490+00:00 app[web.1]: Error: spawn pdftk EACCES
2020-01-16T18:02:38.378492+00:00 app[web.1]: at Process.ChildProcess._handle.onexit (internal/child_process.js:264:19)
2020-01-16T18:02:38.378495+00:00 app[web.1]: at onErrorNT (internal/child_process.js:456:16)
2020-01-16T18:02:38.378497+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:81:21)
2020-01-16T18:02:38.378499+00:00 app[web.1]: Emitted 'error' event on ChildProcess instance at:
2020-01-16T18:02:38.378501+00:00 app[web.1]: at Process.ChildProcess._handle.onexit (internal/child_process.js:270:12)
2020-01-16T18:02:38.378503+00:00 app[web.1]: at onErrorNT (internal/child_process.js:456:16)
2020-01-16T18:02:38.378505+00:00 app[web.1]: at processTicksAndRejections (internal/process/task_queues.js:81:21) {
2020-01-16T18:02:38.378507+00:00 app[web.1]: errno: 'EACCES',
2020-01-16T18:02:38.378509+00:00 app[web.1]: code: 'EACCES',
2020-01-16T18:02:38.378511+00:00 app[web.1]: syscall: 'spawn pdftk',
2020-01-16T18:02:38.378513+00:00 app[web.1]: path: 'pdftk',
2020-01-16T18:02:38.378515+00:00 app[web.1]: spawnargs: [ '-', '/app/pdfs/Example.pdf', 'cat', 'output', '-' ]
2020-01-16T18:02:38.378517+00:00 app[web.1]: }
In case anyone else comes across this error "Error: spawn pdftk ENOENT".
This error is most probably because Heroku can't find 'PdfTK' on your system.
Adding this buildpack to Heroku solved it for me:
'https://github.com/fxtentacle/heroku-pdftk-buildpack.git'
(I added it under the 'heroku/nodejs' buldback)
I have an exe and I want to run it from node.js, and pass an argument, except that it doesn't work..
var exec = require('child_process').execFile;
const fs = require('fs');
try {
if (fs.existsSync("./program/bin/Release/program.exe")) {
console.log("Exists")
} else {
console.log("Not Exists")
}
} catch(err) {
console.log(err)
}
setTimeout(function() {
exec('./program/bin/Release/program.exe manual', function(err, data) {
console.log(err)
console.log(data.toString());
});
}, 0);
It definitely exists as it prints exists, and I can run it from a cmd prompt giving manual as an argument. But through node.js it is not working. It comes back with
Error: spawn ./program/bin/Release/program.exe manual ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:264:19)
at onErrorNT (internal/child_process.js:456:16)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'spawn ./program/bin/Release/program.exe manual',
path: './program/bin/Release/program.exe manual',
spawnargs: [],
cmd: './program/bin/Release/program.exe manual'
}
Does anyone know?
Thanks
Arguments should be passed as an array of strings as the second argument, like so:
exec('./program/bin/Release/program.exe', ['manual'], function(err, data) {
console.log(err)
console.log(data.toString());
});
https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback
I need to stream the laptop mic into the WSL environment. Install pulseAudio successfully on both windows and WSL - but my program still can't connect to the mic: (node program under WSL):
var record = require('node-record-lpcm16')
let stream = record.start({
sampleRate: 16000,
channels: 1,
compress: false,
threshold: 0.0001,
thresholdStart: null,
thresholdEnd: null,
silence: '1.0',
verbose: false
})
ERROR IS
events.js:187
throw er; // Unhandled 'error' event
^
Error: spawn rec ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:264:19)
at onErrorNT (internal/child_process.js:456:16)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
Emitted 'error' event on ChildProcess instance at:
at Process.ChildProcess._handle.onexit (internal/child_process.js:270:12)
at onErrorNT (internal/child_process.js:456:16)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'spawn rec',
path: 'rec',
spawnargs: [
'-q', '-r',
16000, '-c',
1, '-e',
'signed-integer', '-b',
'16', '-t',
'wav', '-',
'silence', '1',
'0.1', '0.0001%',
'1', '1.0',
'0.0001%'
]
}