child_process.spawnSync error 'ENOENT' when unzipping a file - node.js

I'm trying to execute a cli command using child_process.spawnSync to unzip a file, but when I run the code it says ENOENT even though the command works fine.
const result = spawnSync(`unzip ${path}`, [
passwordArg, // ''
outputArg, // ''
]);
// console.log(result)
error: Error: spawnSync unzip /<...>/unzipFiles/test/zipp.zip ENOENT
...
errno: -2,
code: 'ENOENT',
syscall: 'spawnSync unzip /<...>/unzipFiles/test/zipp.zip',
path: 'unzip /<...>/unzipFiles/test/zipp.zip',
spawnargs: ['', '']
If I copy and paste and execute the exact value for path, i.e. unzip /<...>/unzipFiles/test/zipp.zip, it runs as expected.
Any help would be much appreciated.

Related

Using laravel browsershot

I installed Browsershot and I'm taking google's image to test it. I prepared the route but it gives an unexpected error.
Symfony\Component\Process\Exception\ProcessFailedException
The command "C:^\nodejs^\node ^"C:^\Users^\AT08877^\it-graph^\vendor^\spatie^\browsershot^\src/../bin/browser.js^" ^"^{^^"url^^":^^"http:^/^/127.0.0.1:8000^/chart^^",^^"action^^":^^"screenshot^^",^^"options^^":^{^^"type^^":^^"png^^",^^"path^^":^^"C:^^\Users^^\AT08877^^\it-graph^^\public^^\deneme.png^^",^^"args^^":^[^],^^"viewport^^":^{^^"width^^":800,^^"height^^":600^}^}^}^"" failed. Exit Code: 1(General error) Working directory: C:\Users\AT08877\it-graph\public Output: ================ Error Output: ================ [Error: ENOENT: no such file or directory, mkdtemp 'undefined\temp\puppeteer_dev_chrome_profile-XXXXXX'] { errno: -4058, code: 'ENOENT', syscall: 'mkdtemp', path: 'undefined\temp\puppeteer_dev_chrome_profile-XXXXXX' }
Route::get('/test',function ()
{
$browser = Browsershot::url('www.google.com')
->setNodeBinary("'C:\Program Files\nodejs\node'")
->setNpmBinary("'C:\Program Files\nodejs\node'")
->bodyHtml();
dd($browser);
});

Is There a way to get the full path of file dependencies in Madge API?

I'm using Magde API (https://github.com/pahen/madge/blob/master/README.md) to generate the dependency graph of my JS file...
Once the dependencies are return as object, the file paths are not clearly documented... I was wondering if there exists a way to tell Madge to specify the full path not the cut it
I want to get the dependencies of this file:
import * as t from 'C:/Users/Tec/todolist_example_jest/req2.js';
function formatName(user) {
return user.firstName + ' ' + user.lastName;
}
const element = (
<h1>
Hello, {formatName(user)}!
</h1>
);
const user = {
firstName: 'Harper',
lastName: 'Perez'
};
As we can see there exists a dependency on this file:
'C:/Users/Tec/todolist_example_jest/req2.js'; But when using madge to get the dependencies ... look below to see what is the output
Here is the code snippet:
madge('file path').then((res) => {
dependencies = res.obj(); // the dependencies as an object
console.log(dependencies);
});
and this is the output:
{
'../../../../todolist_example_jest/req2.js': [],
'simpleElement.jsx': [ '../../../../todolist_example_jest/req2.js' ]
}
../../../../todolist_example_jest/req2.js
node:fs:585
handleErrorFromBinding(ctx);
^
Error: ENOENT: no such file or directory, open '../../../../todolist_example_jest/req2.js'
at Object.openSync (node:fs:585:3)
at Object.readFileSync (node:fs:453:35)
at C:\Users\Tec\React Native Applications\Upload-main\backend\app.js:47:27
at Array.forEach (<anonymous>)
at C:\Users\Tec\React Native Applications\Upload-main\backend\app.js:44:33 {
errno: -4058,
syscall: 'open',
code: 'ENOENT',
path: '../../../../todolist_example_jest/req2.js'
}

Node.js child_process has no access to global modules

I have installed a npm packages globally in my Kubuntu 19.04
$ npm install -g cordova
/home/username/.npm-global/bin/cordova -> /home/username/.npm-global/lib/node_modules/cordova/bin/cordova
+ cordova#9.0.0
updated 1 package in 7.299s
I can access it from shell
$ cordova -v
9.0.0 (cordova-lib#9.0.1)
But I can't access it from a simple node script with spawn:
const { spawn } = require( 'child_process' );
const cmd = spawn( 'cordova', [ '-v' ] );
cmd.stdout.on( 'data', data => {
console.log( `stdout: ${data}` );
} );
Running it results in the following:
$ node test1.js
events.js:298
throw er; // Unhandled 'error' event
^
Error: spawn cordova ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:267:19)
at onErrorNT (internal/child_process.js:467: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:467:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: -2,
code: 'ENOENT',
syscall: 'spawn cordova',
path: 'cordova',
spawnargs: [ '-v' ]
}
But when using absolute path:
const { spawn } = require( 'child_process' );
const cmd = spawn( '/home/username/.npm-global/bin/cordova', [ '-v' ] );
cmd.stdout.on( 'data', data => {
console.log( `stdout: ${data}` );
} );
I get the expected result:
$ node test1.js
stdout: 9.0.0 (cordova-lib#9.0.1)
My script above is just a testcase, since I have problem to add/run cordova specific stuff in vue-cli-plugin-cordova and quasar. I tracked it down, that the global path is the problem.
Update
I think I found the problem, but not sure how to solve it. When setting /bin/bash as shell, it works:
cmd = spawn('cordova', ['-v'], {
shell: '/bin/bash'
});
Not sure, why this is needed and why the packages vue-cli-plugin-cordova and quasar don't do it.
Ok, after hours of research and tryouts, I have uninstalled node, installed nvm and latest node, set
nvm use --delete-prefix v13.7.0 --silent >> /dev/null
and added this to ~/.bashrc
and now everything works
how I solve my error, I changed the environment variable PATH: to C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;{plus program paths}
which is
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files\nodejs
in my PATH

Aws lambda binary file EACCES error

I got Error: spawn EACCES error at the following line when I tried to execute this in aws lambda.
var zip = childProcess.spawn('zip', [
'-r',
job.destination.name,
'./'
], {
cwd: temporaryDirectoryPath
});
I have a binary file 'zip'.
Full error trace:
Error: spawn EACCES
at exports._errnoException (util.js:1018:11)
at ChildProcess.spawn (internal/child_process.js:319:11)
at Object.exports.spawn (child_process.js:378:9)
at createCompressedFile (/var/task/index.js:141:32)
at /var/task/node_modules/async/lib/async.js:718:13
at iterate (/var/task/node_modules/async/lib/async.js:262:13)
at /var/task/node_modules/async/lib/async.js:274:29
at /var/task/node_modules/async/lib/async.js:44:16
at /var/task/node_modules/async/lib/async.js:723:17
at /var/task/node_modules/async/lib/async.js:167:37
Finally, it worked for me. So all errors like EACCES, ENOEN... has gone.
child_process.spawnSync('mybinary', [], {
shell: true
})

spawnSync('npm', ['install']) gives [Error: spawnSync npm ENOENT]

I am having an issue with spawnSync is giving me ENOENT with simple "npm install". Can someone please help me?
======= NODE SCRIPT ==========
var child = require('child_process').spawnSync('npm', ['install']);
console.log(child.error);
===== OUTPUT ==========
[Error: spawnSync npm ENOENT]
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawnSync npm',
path: 'npm',
spawnargs: [ 'install' ]
only on windows but not on OS X.
This happens on
windows 7 x64
node version: 4.4.3
npm version: 2.15.1
I figured out the issue. On Windows, some commands need to be suffixed with .cmd in order to work. In this example, this updated command works for me:
require('child_process').spawnSync('npm.cmd', ['install']);
Or you can use cross-spawn to make it work cross-platform
I solved this by specifying the shell to be the v1 32-bit Powershell executable.
I don't know if there is a more reliable method of getting the Powershell executable path, but this is what I did:
const { spawnSync } = require('child_process')
const path = require('path')
const shell = process.platform === 'win32'
? 'C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe'
: undefined
const r0 = spawnSync('pnpm', ['run', 'build'], {
cwd: path.join(__dirname, '..', 'projects', 'project-name'),
env: process.env,
stdio: 'inherit',
shell
})
if (r0.error) {
console.log(r0.error)
process.exit(1)
}

Resources