Executing nodejs script file in PHP using exec()

32,790

Solution 1

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");

Solution 2

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

Solution 3

  1. check whether the modules are installed (npm install)
  2. run the execin the node application current working directory:
exec("cd ". dirname($nodeJsPath). " && node nodefunc.js 2>&1", $out, $err);
Share:
32,790

Related videos on Youtube

Abhinav
Author by

Abhinav

I am passionate about Cyber Security, Networking, Computer Architecture and have the thirst for learning how things work at a low level. A programmer who loves to code in PHP and Javascript, alongside a cup of tea. As of now, getting my hands dirty with React Native and loving every moment of it

Updated on September 13, 2022

Comments

  • Abhinav
    Abhinav over 1 year

    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)
    )
    
    • Abhinav
      Abhinav over 7 years
      May I know why the downvote and whats wrong with this question?
    • krampstudio
      krampstudio over 7 years
      Does it work when running the command from CLI ?
    • Abhinav
      Abhinav over 7 years
      Yeah it does run properly when executed from the terminal
  • Abhinav
    Abhinav over 7 years
    hi..it may sound a bit naive but i am not well versed with nodejs, where should I exactly execute npm install because I can see those installed modules folder in this path /usr/lib/node_modules
  • Abhinav
    Abhinav over 7 years
    I am still getting the same error even after changing the directory in the exec command
  • krampstudio
    krampstudio over 7 years
    you need to run npm install into the folder that contains your node project (the project with your package.json file)
  • Abhinav
    Abhinav over 7 years
    Mine actually is not a node project, its a codeigniter project comepletely in PHP, with with just one js file which has nodejs code since I can achieve one particular functionality only by using nodejs
  • Abhinav
    Abhinav over 7 years
    I have installed those modules globally and the package.json is in this folder: /usr/lib/node_modules/npm/package.json. I am clueless as to what to do
  • krampstudio
    krampstudio over 7 years
    I think the issue is because your dependencies are installed globally instead of locally, because I don't think php's exec knows the environment variable NODE_PATH
  • Abhinav
    Abhinav over 7 years
    yes bro...I had to include the absolute path of the module..thanx anyway
  • nssmart
    nssmart about 2 years
    guess it's worth mentioning the solution was proposed by @krampstudio :)