Creating a child process of a bash file in node - node.js

Below I have a string with newlines line, this string is the contents of a bash file. I was wondering if there was a way to run this contents without creating the bash file. I would like to do something like below, where I just pipe the contents to the sh command. But this blew up with the error below.
let string = "\necho \'hello-world\'\n"
execAsync(`${string} | sh`)
I know I can create a temp file / exec it with the path equal to the current working dir / delete the file. But I'd rather not go through the trouble if there's a way to run the script without creating a file.
Error:
{ Error: Command failed:
echo 'hello-world'
| sh
/bin/sh: -c: line 2: syntax error near unexpected token `|'
/bin/sh: -c: line 2: ` | sh'
at ChildProcess.exithandler (child_process.js:207:12)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:852:16)
at Socket.<anonymous> (internal/child_process.js:323:11)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at Pipe._handle.close [as _onclose] (net.js:492:12)
cause:
{ Error: Command failed:
echo 'hello-world'
| sh
/bin/sh: -c: line 2: syntax error near unexpected token `|'
/bin/sh: -c: line 2: ` | sh'
at ChildProcess.exithandler (child_process.js:207:12)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:852:16)
at Socket.<anonymous> (internal/child_process.js:323:11)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at Pipe._handle.close [as _onclose] (net.js:492:12)
killed: false,
code: 2,
signal: null,
cmd: '\necho \'hello-world\'\n | sh' },
isOperational: true,
killed: false,
code: 2,
signal: null,
cmd: '\necho \'hello-world\'\n | sh' }

Pass the script contents to the shell through the -c option. It is supported by most shells.
Example:
var execAsync = require('exec-async');
let script = "echo \'hello\' $(whoami)\nls -l\necho 'bye'"
execAsync('sh', ['-c', script]).then(console.log).catch(console.log);

Related

Unable to convert pdf to image on AWS Lambda

I have a Nodejs 8.10 lambda which converts pdf to png which was working fine untill few days ago.
This is the error which seems to be caused because of AWS update
https://aws.amazon.com/blogs/compute/upcoming-updates-to-the-aws-lambda-execution-environment/
{
Error: Command failed: convert -density 200 -quality 75 "/tmp/temp_file.pdf[0]" "/tmp/temp_file-0.png"
convert: unable to load module `/usr/lib64/ImageMagick-6.7.8/modules-Q16/coders/pdf.la': file not found # error/module.c/OpenModule/1278.
convert: no decode delegate for this image format `/tmp/temp_file.pdf' # error/constitute.c/ReadImage/544.
convert: no images defined `/tmp/temp_file-0.png' # error/convert.c/ConvertImageCommand/3046.
}
The error was resolved by adding this public layer which will stop working after July 22:
arn:aws:lambda:::awslayer:AmazonLinux1703
I tried creating ImageMagick Static Binaries for AWS Lambda.
I followed these instructions for ImageMagic version 6.9.10-5
https://gist.github.com/bensie/56f51bc33d4a55e2fc9a
https://imagemagick.org/download/ImageMagick-6.9.10-55.tar.gz
Folder structure for lambda:
I tried basic resize of png image:
var IM_PATH = "var/task/imagemagick/bin/";
process.env['LD_LIBRARY_PATH'] = 'var/task/imagemagick/lib/';
process.env['PATH'] = process.env['PATH'] + ':' + IM_PATH + ':' + process.env['LD_LIBRARY_PATH'];
var gm = require('gm').subClass({
imageMagick: true,
appPath: 'var/task/imagemagick/bin/',
});
gm('image.png')
.resize(240, 240, '!')
.write('/tmp/resize.png', function (err) {
if (!err) console.log('done');
else {
console.log(err);
}
});
Tried running on Node 8.10 and node 10.x lambdas and here the the errors:
1) Node 8.10
{ Error: Command failed: convert: UnableToOpenConfigureFile `delegates.xml' # warning/configure.c/GetConfigureOptions/677.
convert: NoDecodeDelegateForThisImageFormat `PNG' # error/constitute.c/ReadImage/560.
convert: NoImagesDefined `/tmp/resize.png' # error/convert.c/ConvertImageCommand/3235.
at ChildProcess.onExit (/var/task/node_modules/gm/lib/command.js:301:17)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at maybeClose (internal/child_process.js:925:16)
at Socket.stream.socket.on (internal/child_process.js:346:11)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at Pipe._handle.close [as _onclose] (net.js:567:12) code: 1, signal: null }
2) Node 10.x
{ Error: Command failed: var/task/imagemagick/bin/convert: error while loading shared libraries: libgomp.so.1: cannot open shared object file: No such file or directory
at ChildProcess.onExit (/var/task/node_modules/gm/lib/command.js:301:17)
at ChildProcess.emit (events.js:189:13)
at ChildProcess.EventEmitter.emit (domain.js:441:20)
at maybeClose (internal/child_process.js:970:16)
at Socket.stream.socket.on (internal/child_process.js:389:11)
at Socket.emit (events.js:189:13)
at Socket.EventEmitter.emit (domain.js:441:20)
at Pipe._handle.close (net.js:597:12) code: 127, signal: null }

fs.watch auto reload commands on discord bot

I have been working on my Discord Bot Rxiqi and I've been wanting auto-reload commands when the file has changed... my code is
var folder = "./commands/";
fs.watch(folder, { encoding: 'UTF-8' }, (eventType, filename) => {
if (eventType ==="change") {
console.log(`Updating Command: ${folder+filename}`)
delete require.cache[require.resolve(folder+filename)];
client.commands.delete(folder+filename);
const props = require(filename);
client.commands.set(folder+filename,props);
}
});
But I keep getting this console output:
Updating Command: ./commands/adverts.js
Error: Cannot find module './commands/adverts.js'
at Function.Module._resolveFilename (module.js:547:15)
at Function.resolve (internal/module.js:18:19)
at FSWatcher.fs.watch (E:\BotDevelopment\Rxiqi\status\ready.js:62:38)
at emitTwo (events.js:126:13)
at FSWatcher.emit (events.js:214:7)
at FSEvent.FSWatcher._handle.onchange (fs.js:1364:12)
Updating Command: ./commands/adverts.js
Error: Cannot find module './commands/adverts.js'
at Function.Module._resolveFilename (module.js:547:15)
at Function.resolve (internal/module.js:18:19)
at FSWatcher.fs.watch (E:\BotDevelopment\Rxiqi\status\ready.js:62:38)
at emitTwo (events.js:126:13)
at FSWatcher.emit (events.js:214:7)
at FSEvent.FSWatcher._handle.onchange (fs.js:1364:12)
Any help is very much appreciated :)
What OS are you on? If you're on windows there is a problem as your '/' will not work

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
})

Getting Error: spawn ./node_modules/.bin/grunt ENOENT when running grunt

I have a project that is started in development by
yarn start
It runs a index.js that starts a grunt process and get this error:
$ yarn start
yarn start v0.23.2
$ node ./development
grunt_arguments [ '--force', '--notify', '--verbose', '--debug', '--stack' ]
=======================================
Open http://localhost:8000 to start developing
events.js:141
throw er; // Unhandled 'error' event
^
Error: spawn ./node_modules/.bin/grunt ENOENT
at exports._errnoException (util.js:907:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:189:32)
at onErrorNT (internal/child_process.js:355:16)
at nextTickCallbackWith2Args (node.js:458:9)
at process._tickCallback (node.js:372:17)
at Function.Module.runMain (module.js:443:11)
at startup (node.js:139:18)
at node.js:990:3
error Command failed with exit code 1.
No idea what can it be. Environment is:
Win10
Running with MINGW64
I usually monkey patch child_process to help me debug that kind of issues. Add something like this in the beginning of your index.js file:
const util = require('util')
const childProcess = require("child_process");
const originalSpawn = childProcess.spawn;
childProcess.spawn = function() {
console.trace('SPAWN');
console.log('ARGS');
console.log(util.inspect(arguments, false, null)); // showHidden = false, depth = null
return originalSpawn.apply(this, arguments);
};
If your run, childProcess.spawn('ls', ['-lh', '/usr']) you will see something like:
Trace: SPAWN
at Object.childProcess.spawn (repl:2:9)
at myFunction (repl:2:14)
at repl:1:1
at REPLServer.defaultEval (repl.js:164:27)
at bound (domain.js:250:14)
at REPLServer.runBound [as eval] (domain.js:263:12)
at REPLServer.<anonymous> (repl.js:392:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:210:10)
ARGS
{ '0': 'ls', '1': [ '-lh', '/usr' ] }
Maybe after running it you can update your question with the new information.

getting error 'assetmanager.process is not a function' while trying to start node server

I am trying to run MEAN stack, I am trying to start Node server by running node server command,
`TypeError: assetmanager.process is not a function
at configureApp (C:\Users\Harsh\first\node_modules\meanio\lib\core_modules\module\aggregation.js:401:29)
at Consumer.Dependable.runAction (C:\Users\Harsh\first\node_modules\lazy-dependable\index.js:73:22)
at Consumer.Dependable.fire (C:\Users\Harsh\first\node_modules\lazy-dependable\index.js:70:53)
at Consumer.onResolved (C:\Users\Harsh\first\node_modules\lazy-dependable\index.js:120:8)
at Consumer.Dependable.resolve (C:\Users\Harsh\first\node_modules\lazy-dependable\index.js:56:10)
at Meanio.Container.notifyResolved (C:\Users\Harsh\first\node_modules\lazy-dependable\index.js:209:7)
at Dependency.onResolved (C:\Users\Harsh\first\node_modules\lazy-dependable\index.js:105:18)
at Dependency.Dependable.resolve (C:\Users\Harsh\first\node_modules\lazy-dependable\index.js:56:10)
at Meanio.Container.register (C:\Users\Harsh\first\node_modules\lazy-dependable\index.js:167:5)
at C:\Users\Harsh\first\node_modules\meanio\lib\core_modules\db\index.js:101:20
at open (C:\Users\Harsh\first\node_modules\mongoose\lib\connection.js:488:17)
at NativeConnection.Connection.onOpen (C:\Users\Harsh\first\node_modules\mongoose\lib\connection.js:498:5)
at C:\Users\Harsh\first\node_modules\mongoose\lib\connection.js:457:10
at C:\Users\Harsh\first\node_modules\mongoose\lib\drivers\node-mongodb-native\connection.js:60:5
at C:\Users\Harsh\first\node_modules\mongodb\lib\db.js:229:5
at connectHandler (C:\Users\Harsh\first\node_modules\mongodb\lib\server.js:279:7)
at g (events.js:260:16)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at C:\Users\Harsh\first\node_modules\mongodb-core\lib\topologies\server.js:409:23
at C:\Users\Harsh\first\node_modules\mongodb-core\lib\topologies\server.js:778:13
at Callbacks.emit (C:\Users\Harsh\first\node_modules\mongodb-core\lib\topologies\server.js:95:3)
C:\Users\Harsh\first\node_modules\mongodb\lib\server.js:282
process.nextTick(function() { throw err; })
^
TypeError: assetmanager.process is not a function
at configureApp (C:\Users\Harsh\first\node_modules\meanio\lib\core_modules\module\aggregation.js:401:29)
at Consumer.Dependable.runAction (C:\Users\Harsh\first\node_modules\lazy-dependable\index.js:73:22)
at Consumer.Dependable.fire (C:\Users\Harsh\first\node_modules\lazy-dependable\index.js:70:53)
at Consumer.onResolved (C:\Users\Harsh\first\node_modules\lazy-dependable\index.js:120:8)
at Consumer.Dependable.resolve (C:\Users\Harsh\first\node_modules\lazy-dependable\index.js:56:10)
at Meanio.Container.notifyResolved (C:\Users\Harsh\first\node_modules\lazy-dependable\index.js:209:7)
at Dependency.onResolved (C:\Users\Harsh\first\node_modules\lazy-dependable\index.js:105:18)
at Dependency.Dependable.resolve (C:\Users\Harsh\first\node_modules\lazy-dependable\index.js:56:10)
at Meanio.Container.register (C:\Users\Harsh\first\node_modules\lazy-dependable\index.js:167:5)
at C:\Users\Harsh\first\node_modules\meanio\lib\core_modules\db\index.js:101:20
at open (C:\Users\Harsh\first\node_modules\mongoose\lib\connection.js:488:17)
at NativeConnection.Connection.onOpen (C:\Users\Harsh\first\node_modules\mongoose\lib\connection.js:498:5)
at C:\Users\Harsh\first\node_modules\mongoose\lib\connection.js:457:10
at C:\Users\Harsh\first\node_modules\mongoose\lib\drivers\node-mongodb-native\connection.js:60:5
at C:\Users\Harsh\first\node_modules\mongodb\lib\db.js:229:5
at connectHandler (C:\Users\Harsh\first\node_modules\mongodb\lib\server.js:279:7)
at g (events.js:260:16)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at C:\Users\Harsh\first\node_modules\mongodb-core\lib\topologies\server.js:409:23
at C:\Users\Harsh\first\node_modules\mongodb-core\lib\topologies\server.js:778:13
at Callbacks.emit (C:\Users\Harsh\first\node_modules\mongodb-core\lib\topologies\server.js:95:3)
Also used gulp command still same error, where am I going wrong??
Had a similar error when there was an issue with the git clone. Re-clone the repository and then run on your console:
git clone https://github.com/meanjs/mean.git myprojectname
npm install
bower install
The install processes will take a little while but running gulp after this should work.

Resources