Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing nodejs script file in PHP using exec()

Tags:

In my Application, I want to execute a Node.JS file from PHP which in turn makes an HTTP request to another PHP file.

In short this is the Process that I am doing.

PHP file--->calls--> Nodejs file--->processes data -->and makes http request to-->PHP File

When I run the nodejs file via terminal, it successfully makes the http request to another PHP file and I get what I want.

But, when I try to run the nodejs file through PHP, the nodejs files is not able to find some modules.

My code in PHP:

$nodeJsPath = '/var/www/html/projectfolder/js/nodefunc.js';  $ret = exec("node ".$nodeJsPath.' 2>&1', $out, $err); 

This is the error that I am getting:

Array (     [0] => module.js:457     [1] =>     throw err;     [2] =>     ^     [3] =>      [4] => Error: Cannot find module 'some_module'     [5] =>     at Function.Module._resolveFilename (module.js:455:15)     [6] =>     at Function.Module._load (module.js:403:25)     [7] =>     at Module.require (module.js:483:17)     [8] =>     at require (internal/module.js:20:19)     [9] =>     at Object.<anonymous> (/var/www/html/projectfolder/js/nodefunc.js:5:9)     [10] =>     at Module._compile (module.js:556:32)     [11] =>     at Object.Module._extensions..js (module.js:565:10)     [12] =>     at Module.load (module.js:473:32)     [13] =>     at tryModuleLoad (module.js:432:12)     [14] =>     at Function.Module._load (module.js:424:3) ) 
like image 361
Abhinav Avatar asked Sep 15 '16 07:09

Abhinav


People also ask

How do I run a node js script in PHP?

js instance by going to: http://example.org/node.php?start=node_modules/jt-js-sample/index.js . Now you can request your app by browsing to: http://example.org/node.php?path=optional/request/path . This will return a response from the running node. js app at http://127.0.0.1:49999/optional/request/path .

How do I run an exec in node JS?

The exec() function in Node. js creates a new shell process and executes a command in that shell. The output of the command is kept in a buffer in memory, which you can accept via a callback function passed into exec() .

How do I run a node js script?

The usual way to run a Node. js program is to run the globally available node command (once you install Node. js) and pass the name of the file you want to execute. While running the command, make sure you are in the same directory which contains the app.


2 Answers

Your goal is to execute a node command after changing directory. Thus, you will need to run multiple sequential commands via the PHP exec() function.

Commands:

  1. cd /var/www/html/projectfolder/js
  2. node nodefunc.js 2>&1

This is possible by adding && or ; in between the commands.

$ret = exec("cd /var/www/html/projectfolder/js; node nodefunc.js 2>&1", $out, $err); 
like image 137
Haresh Vidja Avatar answered Sep 19 '22 18:09

Haresh Vidja


I got it finally. Its just ignorng the NODE_PATH variable for reasons unkown :(

In the Nodejs File I hade to give the absolute path of the module like this:

var request = require("/usr/lib/node_modules/request"); 
like image 21
Abhinav Avatar answered Sep 20 '22 18:09

Abhinav