0

I have a AWS EC2 instance containing a CentOS (atleast that's what I think) and I followed this link(How to yum install Node.JS on Amazon Linux) to install nodejs on my instance (the second answer).The thing is I am able to install it successfuly and even able to run the following commands

node --version
v8.11.1

npm --version
5.6.0

node -e "console.log('Running Node.js ' + process.version)"
Running Node.js v8.11.1

But I have a python file that runs a node command through python's subprocess module.This the python file below

import json
import yaml
import subprocess


def output_filter(json_data, exp):
    json_data = json.dumps(json_data)
    result = subprocess.check_output(['node', '/home/ec2-user/api-demo/jstest3.js', json_data, exp])
    dict = yaml.load(result.decode("utf-8"))
    filtered_output = json.dumps(dict)
    return json.loads(filtered_output)

Below is my jstest3.js file

var args = process.argv.slice(2);
//console.log(args[0]);

var jsonata = require("jsonata");

var data = JSON.parse(args[0]);
var expression = jsonata(args[1]);
var result = expression.evaluate(data);
console.log(result);

I basically uses a nodejs library called jsonata to do some JSON parsing.I am able to use execute this python file in my local machine (I have an Ubuntu 16.10 x86_64) without any issues.But when I execute this in my aws instance, this is the error I get

result = subprocess.check_output(['node', '/home/ec2-user/api-demo/jstest3.js', json_data, exp])
  File "/usr/lib64/python3.5/subprocess.py", line 316, in check_output
    **kwargs).stdout
  File "/usr/lib64/python3.5/subprocess.py", line 383, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/lib64/python3.5/subprocess.py", line 676, in __init__
    restore_signals, start_new_session)
  File "/usr/lib64/python3.5/subprocess.py", line 1289, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'node'

Why am I getting this error despite node being installed in my aws instance as shown above?What am I doing wrong?

Souvik Ray
  • 2,899
  • 5
  • 38
  • 70
  • How are you executing your python file? – Christian Saiki Apr 03 '18 at 15:04
  • @csaiki actually its a flask app.So this file only gets called when needed.However you can individually run it by adding a statement like `output_filter({}, "sys.log")` and then exeuting the file `python3 file.py` – Souvik Ray Apr 03 '18 at 15:16
  • Oh ok, Souvik, I was asking that because the user that is executing the python file and the javascript file does not have access to node. Perhaps the best way is to execute the file is by calling the whole path of the node binary. Execute `which node` to print what is the path to the binary – Christian Saiki Apr 03 '18 at 16:05
  • @csaiki hey great idea!Unfortunately it still doesn't work. – Souvik Ray Apr 03 '18 at 18:24

1 Answers1

0

Ah! For some reason the AWS instance couldn't keep up with all the installations I did.So once I restarted the instance by going to the AWS console -> restart instance, all the errors were suddenly gone and it started processing my nodejs requests.

Souvik Ray
  • 2,899
  • 5
  • 38
  • 70