Run python script using node js error no 22 - javascript

I'm using node.js to run a python script through the web browser. below is the ScriptRunner.js code.
var PythonShell = require('python-shell');
var path = require('path');
exports.runScript = function() {
var options = {
scriptPath: 'D:\L4Project\working code\finalproject\routes'
};
PythonShell.run('sensitive2.py',options, function (err, results) {
if (err) throw err;
console.log('results: %j', results);
});
}
when I run the script in browser it returns error message like below.
D:\L4Project\working code\finalproject\routes\scriptRunner.js:10
if (err) throw err;
^
outes\sensitive2.py': [Errno 22] Invalid argumentg codeinalproject at PythonShell.parseError (D:\L4Project\working code\finalproject\node_modules\python-shell\index.js:191:17)
at terminateIfNeeded (D:\L4Project\working code\finalproject\node_modules\python-shell\index.js:98:28)
at ChildProcess.<anonymous> (D:\L4Project\working code\finalproject\node_modules\python-shell\index.js:89:9)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)
I have googles lot and nothing worked. Can Somebody help me to solve the problem?

Related

ffmpeg module saving not working after compression

I'm just trying to save an mp4 to a different mp4 (before I even start playing around with the different compression settings). What exactly is going wrong here?
const ffmpeg = require('ffmpeg');
try {
var process = new ffmpeg('./original.mp4');
process.then(function (video) {
video
.save('./new.mp4', function (error, file) {
if (!error) {
console.log('Video file: ' + file);
} else {
console.log(error)
}
});
}, function (err) {
console.log('Error: ' + err);
});
} catch (e) {
console.log(e.code);
console.log(e.msg);
}
I get the following error:
Error: Command failed: ffmpeg -i ./original.mp4 ./new.mp4
/bin/sh: ffmpeg: command not found
at ChildProcess.exithandler (child_process.js:390:12)
at ChildProcess.emit (events.js:400:28)
at maybeClose (internal/child_process.js:1055:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5) {
killed: false,
code: 127,
signal: null,
cmd: 'ffmpeg -i ./original.mp4 ./new.mp4'
}
It seems that the library you're using tries to call an ffmpeg executable but cannot find it.

Unable to execute openssl on AWS lambda: Openssl seems to be removed from node10.x lambda runtime

I am trying to use https://www.npmjs.com/package/passbook to generate iOS passbook using lambda. The package has the following snippet to sign the manifest:
var sign = execFile("openssl", args, { stdio: "pipe" }, function(error, stdout, stderr) {
var trimmedStderr = stderr.trim();
// Windows outputs some unhelpful error messages, but still produces a valid signature
if (error || (trimmedStderr && trimmedStderr.indexOf('- done') < 0)) {
callback(new Error(error || stderr));
} else {
var signature = stdout.split(/\n\n/)[3];
callback(null, new Buffer(signature, "base64"));
}
});
But when I executed it on AWS Lambda (NodeJs 10), I got the following error:
Error: spawn openssl ENOENT
at /var/task/node_modules/passbook/lib/pass.js:361:16
at exithandler (child_process.js:301:5)
at ChildProcess.errorhandler (child_process.js:313:5)
at ChildProcess.emit (events.js:189:13)
at ChildProcess.EventEmitter.emit (domain.js:441:20)
at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12)
at onErrorNT (internal/child_process.js:415:16)
at process._tickCallback (internal/process/next_tick.js:63:19)
I am thinking the AMI image used does not have openssl installed. Does anyone know if openssl installed and where it is located?

Fs Module error

I'm using Node v6.7.0 trying to use 'fs' module but there is error as you see below. i Have tried to install it in addition but is not working(even if i add whole path). If i check in the site https://www.npmjs.com/package/fs you can see the message. Any ideas how can use the module?
var filename = process.argv[2];
var version = process.argv[3];
var fs = require('fs');
var prompt = require('C:/Program Files/nodejs/node_modules/prompt');
var p4 = require('C:/Program Files/nodejs/node_modules/p4');
p4.edit(filename, function(err, data) {
if (err) {
console.error(err.message);
}
fs.readFile(filename, 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
var result = data.replace(/string to be replaced/g, version);
fs.writeFile(filename, result, 'utf8', function (err) {
if (err) return console.log(err);
});
});
console.log(data);
prompt.start();
prompt.get('p4 submit -c changelist', function (err, result) {
if(err) {
console.log(err.message);
}
console.log(result);
});
});
fs.js:303
binding.open(pathModule._makeLong(path),
^
TypeError: path must be a string or Buffer
at TypeError (native)
at Object.fs.readFile (fs.js:303:11)
at C:\WorkSpace\http.js:22:9
at C:\Program Files\nodejs\node_modules\p4\p4.js:13:24
at ChildProcess.exithandler (child_process.js:213:5)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:877:16)
at Socket.<anonymous> (internal/child_process.js:334:11)
at emitOne (events.js:96:13)
Process finished with exit code 1
fs is a nodejs core module : here is the fs documentation
I found the answer it should be executed in node command line and var filename = process.argv[2]; must be filled up.

Error: spawn EACCES in ubuntu for node.js/express app trying to run python

I have made a node.js server to run a python script using python-shell and am encountering an EACCES error ever since migrating from windows to Ubuntu. I have to my knowledge and limited ability tried to set the correct permissions to no avail, and have not currently found anyone with a problem such as this with a server trying to run another script. My question is how do I stop such an error occuring?
EDIT: Added JavaScript code, Image of ls-l in main server folder, and js script folder.
Express HTML error log (modified):
<h1>spawn EACCES</h1>
<h2></h2>
<pre>Error: spawn EACCES
at exports._errnoException (util.js:870:11)
at ChildProcess.spawn (internal/child_process.js:298:11)
at exports.spawn (child_process.js:362:9)
at new PythonShell (/home/user_name/simple_server/node_modules/python-shell/index.js:59:25)
at run_py_script (/home/user_name/simple_server/routes/rain_track.js:11:19)
at /home/user_name/simple_server/routes/rain_track.js:43:5
at Layer.handle [as handle_request] (/home/user_name/simple_server/node_modules/express/lib/router/layer.js:95:5)
at next (/home/user_name/simple_server/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/user_name/simple_server/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/user_name/simple_server/node_modules/express/lib/router/layer.js:95:5)</pre>
Javascript code:
var express = require('express');
var router = express.Router();
var PythonShell = require('python-shell');
var options = {
mode: 'json',
pythonPath: '/home/user_name/simple_server'
};
function run_py_script(data, res, callback){
var pyshell = new PythonShell('dummy.py', options);
console.log(data.latitude + ", " + data.longitude + "\n");
pyshell.send(""+data.latitude + ", "+ data.longitude+"\n"); // change to data
pyshell.on('message', function(message){
console.log(message);
res.json(message);
});
pyshell.end(function(err){
if (err) {
console.log("Python script error has occured.");
console.log(err);
}
//return err ? callback(null) : callback(null, ret_val);
});
}
/* GET rain_track data. */
router.post('/', function(req, res, next) {
console.log("Location got: "+req.body.coords.latitude + ", " + req.body.coords.longitude);
var location_data = {
"latitude" : req.body.coords.latitude,
"longitude" : req.body.coords.longitude
};
run_py_script(location_data, res, function(err, rain_data) {
if (err){ console.log("error in python script" ); return res.json(null); }
})
});
module.exports = router;
ls -l:
Upon detailed testing I have been unable to find a solution to this issue, and the question has indeed died.
However I successfully found a work-around, finding the issue with the EACCES was with the python path (in the options object). Removing this and running a python script in the server terminal's run location (e.g. /bin directory) would still work fine.

Node fsmkdirSync error

I have the following folder structure:
Inside of CacheModule.js i have the following code:
socket.on('saveUserCache', function (obj, user_id) {
var jsonOutput = JSON.stringify(obj);
if (!fs.existsSync('./cacheObjects/' + user_id)) {
fs.mkdirSync('./cacheObjects/' + user_id, 0777, function (err) {
if (err) {
console.log(err);
}
});
}
fs.writeFile('./cacheObjects/' + user_id + '/cache.json', jsonOutput, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
});
The idea behind this is to check if the users folder exists (if not) create a new folder and then write to a file.
However im getting the following error:
Error: ENOENT: no such file or directory, mkdir './cacheObjects/125'
at Error (native)
at Object.fs.mkdirSync (fs.js:794:18)
at Socket.<anonymous> (/var/www/project/app_server/costum_modules/CacheModule.js:10:16)
at emitTwo (events.js:87:13)
at Socket.emit (events.js:172:7)
at Socket.onevent (/var/www/project/app_server/node_modules/socket.io/lib/socket.js:330:8)
at Socket.onpacket (/var/www/project/app_server/node_modules/socket.io/lib/socket.js:290:12)
at Client.ondecoded (/var/www/project/app_server/node_modules/socket.io/lib/client.js:193:14)
at Decoder.Emitter.emit (/var/www/project/app_server/node_modules/socket.io/node_modules/socket.io-parser/node_modules/component-emitter/index.js:134:20)
at Decoder.add (/var/www/project/app_server/node_modules/socket.io/node_modules/socket.io-parser/index.js:247:12)
at Client.ondata (/var/www/project/app_server/node_modules/socket.io/lib/client.js:175:18)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at Socket.onPacket (/var/www/project/app_server/node_modules/socket.io/node_modules/engine.io/lib/socket.js:99:14)
at emitOne (events.js:82:20)
at WebSocket.emit (events.js:169:7)
Can anyone tell me why this is happening. it looks like its sending mkdir with the string. Also i have given the right permissions (so it is not lacking permissions)

Categories