How to catch EPERM error on child process - javascript

Sometimes I've been getting this error while using puppeteer but since I haven't found a way around it. I just wrote code that will unlink it after the browser closes. Although sometimes Puppeteer will crash the whole process when this happens.
Is there a way to detect this crash and be able to do error handling?
[Error: EPERM: operation not permitted, unlink 'C:\Users\user\AppData\Local\Temp\puppeteer_dev_chrome_profile-8gwzA9\CrashpadMetrics-active.pma'] {
errno: -4048,
code: 'EPERM',
syscall: 'unlink',
path: 'C:\\Users\\user\\AppData\\Local\\Temp\\puppeteer_dev_chrome_profile-8gwzA9\\CrashpadMetrics-active.pma'
}
Right now I'm using child.on("error", exithandler) but it doesn't seem to work on this error.
var child = spawn(process.execPath, [__filename, "child"], {
stdio: ["inherit", "inherit", "inherit", "ipc"],
});
child.on("error", (err) =>{
console.log(`process ${child.pid} crashed with error: ` + err)
})

Related

nodejs: valid command line prompt doesn't exec or spawn with ENOENT error

I have a bash command (debian 10, GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)):
documents=("/data/sice.pdf" "/das00/12ser.pdf");bash ./clean-pdfs.sh "${documents[*]}"
that works when I paste into terminal.
However invoking it with either exec or spawn fails without giving clear error message.
When I ran it with exec I got some complaints about the brackets. Remembering the output is quite large, I opted for spawn
const { exec } = require('child_process');
command = `documents=(${pdfPaths});` + 'bash ./clean-pdfs.sh "${documents[*]}"'
console.log(command);
const subProcess = require('child_process')
const lsChildProcess = subProcess.spawn(command)
lsChildProcess.stdout.on('data', (data) => {
console.log(data);
})
lsChildProcess.on('error', function(err) {
console.log(err);
});
and after running this nodejs script I get the following error message that isn't very helpful (i changed the paths for security reasons):
{ Error: spawn documents=("/data/Traa.pdf" "/dater.pdf");bash ./clean-pdfs.sh "${documents[*]}" ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
at onErrorNT (internal/child_process.js:415:16)
at process._tickCallback (internal/process/next_tick.js:63:19)
errno: 'ENOENT',
code: 'ENOENT',
syscall:
'spawn documents=("/daice.pdf" "/daer.pdf");bash ./clean-pdfs.sh "${documents[*]}"',
path:
'documents=("/dace.pdf" "/daer.pdf");bash ./clean-pdfs.sh "${documents[*]}"',
spawnargs: [] }
The option shell is required here to (both conditions apply):
parse multiple commands separated by ";"
run commands that are not executable, like assigning an environment variable with document=.
To have the output of the spawned process printed to the console, we can use the option stdio: 'inherit'.
Both settings are documented here: https://nodejs.org/api/child_process.html#child_processspawncommand-args-options
And here is my version of the code that I was able to test succesfully on a zsh terminal. I'm using spawnSync because it's easier to handle without a callback, but spawn works just as well.
const pdfPaths = '"/data/sice.pdf" "/das00/12ser.pdf"';
command = `documents=(${pdfPaths});` + 'bash ./clean-pdfs.sh "${documents[*]}"'
console.log(command);
const subProcess = require('child_process')
subProcess.spawnSync(command, { shell: true, stdio: 'inherit' })

fs.renameSync doesn't work with folder that contains another folder

Simply try to rename a folder using:
fs.renameSync(currPath, newPath, (err) => {
if(err) {
throw err;
}
console.log("Directory renamed successfully.");
});
I get this error:
Error: EPERM: operation not permitted, rename
'C:\inetpub\wwwroot\Server/views/app/files/sources/voices/intermediate/11-2'
-> 'C:\inetpub\wwwroot\Server/views/app/files/sources/voices/intermediate/new-directory-2'
at Object.renameSync (node:fs:978:3)
at Timeout._onTimeout (C:\inetpub\wwwroot\Server\mother\mother.service.js:680:22)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7) { errno: -4048, syscall: 'rename', code: 'EPERM', path:
'C:\inetpub\wwwroot\Server/views/app/files/sources/voices/intermediate/11-2',
dest:
'C:\inetpub\wwwroot\Server/views/app/files/sources/voices/intermediate/new-directory-2'
}
But if the folder I try to rename is empty the function works without issues!!!
How to fix this and rename a folder that contains folders in Nodejs???
Update: I noticed if I try to manually rename things when my server is running I get this error too:

Getting error on gatsby build command: TypeError: Unexpected response from worker: undefined

I am getting this error when I run the command $ gatsby build on the terminal.
What gets my attention on this one is that it mentions jest-worker, and I don't see that module on my package.json. I see it only in the node_modules folder.
I already tried many things. Deleting node_modules, package_lock.json, npm cache clean --force and some other things.
I can't find these errors on the internet either.
If I keep scrolling down on the terminal, the error ends with this:
/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/yoga-layout-prebuilt/yoga-layout/build/Release/nbind.js:53
throw ex;
^
Error: write EPIPE
at process.target._send (internal/child_process.js:806:20)
at process.target.send (internal/child_process.js:676:19)
at /Users/marcelo/Work/SM/gatsby-on-demand/node_modules/gatsby-cli/lib/reporter/loggers/ipc/index.js:58:13
at dispatch (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/gatsby-cli/lib/reporter/redux/index.js:54:5)
at Object.createLog (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/redux/lib/redux.js:483:12)
at Reporter.log (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/gatsby-cli/lib/reporter/reporter.js:173:40)
at Object.console.log (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/gatsby-cli/lib/reporter/patch-console.js:17:14)
at WithHeaderLayout (/Users/marcelo/Work/SM/gatsby-on-demand/public/render-page.js:104712:376)
at d (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:36:498)
at $a (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:39:16)
at a.b.render (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:44:476)
at a.b.read (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:44:18)
at renderToString (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:54:364)
at Module.default (/Users/marcelo/Work/SM/gatsby-on-demand/public/render-page.js:710:28)
at /Users/marcelo/Work/SM/gatsby-on-demand/node_modules/gatsby/dist/utils/worker/render-html.js:28:36
at Promise._execute (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/bluebird/js/release/debuggability.js:384:9)
Emitted 'error' event on process instance at:
at processEmit [as emit] (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/signal-exit/index.js:161:32)
at internal/child_process.js:810:39
at processTicksAndRejections (internal/process/task_queues.js:75:11) {
errno: 'EPIPE',
code: 'EPIPE',
syscall: 'write'
}
I've been going through this the whole day.
Any ideas?

node execFile python script in aws lambda

I am trying to execute a script through a node service hosted on AWS lambda, but consistently get a ENOENT exception.
2020-04-22 07:55:14.613 (-04:00) 9c8c54fc-2aa2-4d17-89d9-ca1e404191b7 ERROR Error: spawn ./bin/test-bin.py 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) {
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'spawn ./bin/test-bin.py',
path: './bin/test-bin.py',
spawnargs: [ 1, 2 ],
cmd: './bin/test-bin.py 1 2'
}
Executing cat bin/test-bin.py in the child process spits out the source code of the script, ls -l through the child process shows that the script is executable, and the same code works locally on my linux machine.
const { execFile } = require('child_process');
execFile('cat', ["bin/test-bin.py"], (err, out) => {
if (err) {
console.error(err)
}
else {
console.log(out)
}
});
The script:
#!/usr/bin/python
import sys
import time
def sum(n1, n2):
return int(n1) + int(n2)
print(sum(sys.argv[1], sys.argv[2]))
Your python script is not a standalone executable - it relies on the shell interpreting the #!/usr/bin/python line at the top of the file to load the python interpreter (/usr/bin/python), and then run the script.
Because execFile doesn't load a shell, it won't do this. You could just use exec instead of execFile, but this is less safe, and slower.
Instead, run /usr/bin/python with execFile, with your script as an argument:
execFile('/usr/bin/python', ['bin/test-bin.py'], (err, out) => {
// ...
});

fs-extra: Error: EPERM: operation not permitted, unlink

I am receiving the following error using fs-extra:
ERROR { [Error: EPERM: operation not permitted, unlink
'C:\Projects\xxx\branches\xxx\release'] errno: -4048, code:
'EPERM', syscall: 'unlink', path:
'C:\Projects\xxx\branches\xxx\release' }
When using this code in my node application:
const fse = require('fs-extra');
fse.copySync('../util/various/a.html', '../release');
fse.copySync('../util/various/b.html', '../release');
I would like to know, what could cause the error and how to fix it.
fs-extra doesn't support copying a file to a directory.
This will work:
const fse = require('fs-extra');
fse.copySync('../util/various/a.html', '../release/a.html');
fse.copySync('../util/various/b.html', '../release/b.html');
This is as-designed (https://github.com/jprichardson/node-fs-extra/issues/320) although I am here because I ran into the same issue.
The module can't remove the destination file because of file's permissions (read-only).

Categories

Resources