Aws lambda, cant run npm module - node.js

I am trying to create a AWS Lambda function to merge pdf files stored in S3, and I am getting the following error:
Error: Command failed: java -jar "/var/task/node_modules/easy-pdf-merge/jar/pdfbox.jar" PDFMerger "samplepdf1.pdf" "samplepdf2.pdf" "merged.pdf"
/bin/sh: java: command not found
at ChildProcess.exithandler (child_process.js:294:12)
at ChildProcess.emit (events.js:198:13)
at maybeClose (internal/child_process.js:982:16)
at Socket.stream.socket.on (internal/child_process.js:389:11)
at Socket.emit (events.js:198:13)
at Pipe._handle.close (net.js:607:12)
killed: false,
code: 127,
signal: null,
cmd:
'java -jar "/var/task/node_modules/easy-pdf-merge/jar/pdfbox.jar" PDFMerger "samplepdf1.pdf" "samplepdf2.pdf" "merged.pdf"' }
I have installed and saved the npm module locally on windows, zipped it all and uploaded as aws lambda function with the following structure
myFunction.zip:
-node_modules
index.js
package.json
package-lock.json

The error is clear, you're trying to call the java command under a node lambda which is not going to work.
I suggest you create another lambda java function that will accept the pdf files to be merged and return the merged file to your original lambda function

Related

Intellij Not finding installed CLI commands - Especially MongoDump/Restore

when i try to run a file with IntelliJ it does not recognize the Mongo CLI commands like mongodump
but what is weird is that when trying to run the same command on my local machine in the terminal it's there, the same goes when trying to do it manually on the terminal tab in IntelliJ ,
i am using zsh
/bin/sh: mongodump: command not found
at ChildProcess.exithandler (node: child process: 397:12)
at ChildProcess.emit (node: events: 390:28)
at ChildProcess.emit (node: domain:475:12)
at maybeClose (node: internal/child process: 1064:16)
at Socket. <anonymous> (node: internal/child process: 450:11)
at Socket.emit (node: events:390:28)
at Socket.emit (node: domain: 475:12)
at Pipe.<anonymous> (node:net:687:12)
at Pipe.callbackTrampoline (node: internal/async hooks:130:17)
this is the message i get when running the file through IntelliJ

ERR_ELECTRON_BUILDER_CANNOT_EXECUTE - package.json: The data present in the reparse point buffer is invalid

I'm using NodeJS 10 and Electron 10 (both of which are quite old). I'm trying to install a local custom module, which I'm including using the answer at How to specify local modules as npm package dependencies. But when I then run
npm install
I see the following error
⨯ open C:\Development\my-project\node_modules\my-custom-module\package.json: The data present in the reparse point buffer is invalid.
github.com/develar/app-builder/pkg/node-modules.(*Collector).resolveDependency
/Volumes/data/Documents/app-builder/pkg/node-modules/nodeModuleCollector.go:192
github.com/develar/app-builder/pkg/node-modules.(*Collector).processDependencies
/Volumes/data/Documents/app-builder/pkg/node-modules/nodeModuleCollector.go:95
github.com/develar/app-builder/pkg/node-modules.(*Collector).readDependencyTree
/Volumes/data/Documents/app-builder/pkg/node-modules/nodeModuleCollector.go:56
github.com/develar/app-builder/pkg/node-modules.ConfigureCommand.func1
/Volumes/data/Documents/app-builder/pkg/node-modules/tree.go:41
github.com/alecthomas/kingpin.(*actionMixin).applyActions
/Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin#v2.2.6+incompatible/actions.go:28
github.com/alecthomas/kingpin.(*Application).applyActions
/Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin#v2.2.6+incompatible/app.go:557
github.com/alecthomas/kingpin.(*Application).execute
/Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin#v2.2.6+incompatible/app.go:390
github.com/alecthomas/kingpin.(*Application).Parse
/Volumes/data/go/pkg/mod/github.com/alecthomas/kingpin#v2.2.6+incompatible/app.go:222
main.main
/Volumes/data/Documents/app-builder/main.go:90
runtime.main
/usr/local/Cellar/go/1.14.2_1/libexec/src/runtime/proc.go:203
runtime.goexit
/usr/local/Cellar/go/1.14.2_1/libexec/src/runtime/asm_amd64.s:1373
⨯ C:\Development\my-project\node_modules\app-builder-bin\win\x64\app-builder.exe exited with code ERR_ELECTRON_BUILDER_CANNOT_EXECUTE stackTrace=
Error: C:\Development\my-project\node_modules\app-builder-bin\win\x64\app-builder.exe exited with code ERR_ELECTRON_BUILDER_CANNOT_EXECUTE
at ChildProcess.childProcess.once.code (C:\Development\my-project\node_modules\builder-util\src\util.ts:243:14)
at Object.onceWrapper (events.js:286:20)
at ChildProcess.emit (events.js:198:13)
at maybeClose (internal/child_process.js:982:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
Any ideas on what could cause this problem? The error message itself seems to pop up in relation to OneDrive.

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.

How to open a .exe file on Linux Hosted NodeJS

I am attempting to have my node server launch a .exe file on a Linux hosted implementation of NODEJS
My .exe is located here: '/home/username/projects/ProjectName/server/webserver/Project.exe'
and when I attempt to start the exe
with:
exec('/home/username/projects/ProjectName/server/webserver/Project.exe', ["/home/username/projects/ProjectName/server/webserver/files/"+date], function(err, data) {
}
i am getting a file not fond error
that looks like this:
{ Error: Command failed: /home/username/projects/ProjectName/server/webserver/Project.exe /home/username/projects/ProjectName/server/webserver/files/31005
/home/username/projects/ProjectName/server/webserver/Project.exe: 1: /home/username/projects/ProjectName/server/webserver/Project.exe: MZ�##���#�#��: not found
/home/username/projects/ProjectName/server/webserver/Project.exe: 2: /home/username/projects/ProjectName/server/webserver/Project.exe: Syntax error: ")" unexpected
at ChildProcess.exithandler (child_process.js:294:12)
at ChildProcess.emit (events.js:198:13)
at maybeClose (internal/child_process.js:982:16)
at Socket.stream.socket.on (internal/child_process.js:389:11)
at Socket.emit (events.js:198:13)
at Pipe._handle.close (net.js:606:12)
killed: false,
code: 2,
signal: null,
cmd:
'/home/username/projects/ProjectName/server/webserver/Project.exe /home/username/projects/ProjectName/server/webserver/files/31005' }
When attempting to run the same function on a windows based NodeJS with windows directories instead of linux it works, the file can be opened via Wine on Linux, is there somewhere in my function call where i need to direct it to open the .exe with WINE? If so how do I go about doing that?
Try this
const { exec} = require('child_process');
exec('wine /home/username/projects/ProjectName/server/webserver/Project.exe /home/username/projects/ProjectName/server/webserver/files/'+date,
function(err, data){
});
https://superuser.com/questions/1058985/launch-program-with-wine-using-exe-arguments
https://stackoverflow.com/a/49069169/12167785
I got my desired functionality using the following:
exec("wine", ["/home/username/projects/ProjectName/server/webserver/Project.exe", date], function(err,data){
}

AWS-CLI An error occurred (403) when calling the HeadObject operation: Forbidden

I have hosted a nodejs Server AWS EC2 instance. My intention is to download(Copy) a zip file from my AWS S3 private Bucket(Please note I need S3 repo to be in private access). For my business requirement I must use shell command(aws-cli) in nodejs to copy zip file from S3 Bucket.
var executeShellCommand = require('child_process').exec,child;
executeShellCommand("aws s3 cp s3://<BUCKET_NAME>/<ZIP_FILE_KEY>",
function (error, stdout, stderr){
if(error){
console.log(error);
}else{
console.log("ZIP file downloaded successfully");
}
});
When the shell command tries to execute, I am getting the following error:
{ Error: Command failed: aws s3 cp s3://<BUCKET_NAME>/<ZIP_FILE_KEY> .
fatal error: An error occurred (403) when calling the HeadObject operation: Forbidden
at ChildProcess.exithandler (child_process.js:204:12)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:891:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
killed: false,
code: 1,
signal: null,
cmd: 'aws s3 cp s3://<BUCKET_NAME>/<ZIP_FILE_KEY> .' }
When i try to run the cp command in EC2 instance command line it was working and the file was download too. Also I have found that aws_access_key_id and aws_secret_access_key credentials should be know to bash command to access the private S3. Please correct me if I am wrong.
What configuration I am missing to make aws cp success via nodejs?

Resources