Executing nodejs script file in PHP using exec()

call node js api from php
php exec
how to include php file in node js
node js in php
express php
run php in electron
php execute shell script
send data from nodejs to php

In my Application, I want to execute a Node.JS file from PHP which in turn makes a 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:

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

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

NodeJs / Php - Extremely simple! Please help asap!!!, I am running NodeJS on my server and I have test.js with: I need to be able to read the result of the script and preferable it would be if I could to run a js file through node and get the return value through PHP using exec() or shell_exec(). PHP Node.js: Execute JavaScript using Node.js from PHP. This class can execute JavaScript using Node.js from PHP. It can take a given JavaScript source file and executes it by running the Node.js program. The class changes the current directory to the path of the given JavaScript file.

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

exec-php, Execute PHP function within NodeJS application. npm install exec-php. Usage. var execPhp = require('exec-php'); execPhp('path/to/php/file.php', '/path/to/php/​bin/php', The callback function called with below arguments :. Run Shell or bash file using Nodejs if you want to execute whole shell script file, instead of commands, Then see the following code, You can use any of the above methods to achieve this functionality.

you have to execute node command after change directory, you have to achive two things in one command

1.change directory cd /var/www/html/projectfolder/js

2.then execute node nodefunc.js 2>&1

use && or ; for execute multiple command in one line as mentioned below

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

niutech/node.php: Run node.js apps from PHP even on a , The node.php script installs an official node.js package, starts a hidden server on localhost:49999 with provided JS file and proxies all requests to it. on Linux with safe mode off and the following functions enabled: curl_exec , exec , passthru . node.php[?path=some/path] - serves an already running node.js app with an  Here, when I run the nodejs file, it will execute the shell file and the output would be: Run. node node_program.js output. Hi There! You can execute any script just by mentioning the shell command or shell script in exec callback. Hope this helps! Happy coding :)

Running locally a PHP program from the browser with Node.js, Node.js is the link that connects the browser to executable files on a local machine. Node launches the execution of the script with the runner.exec command. param = url.parse(request.url).query; var localpath = path.join(​process.cwd(),  @user1748217 you can import the text from a .sql file using Node's fs module. You can then pass this text as the first argument in the .query call. This would only work be default if the file had only one query. This solution ensures you can pass a file that has multiple. – Jacob Penney Mar 13 at 18:40 |

exec - Manual, To get the output of the executed command, be sure to set and use the output parameter. background, the output of the program must be redirected to a file or another output stream. On Windows exec() will first start cmd.exe to launch the command. If you're trying to use exec in a script that uses signal SIGCHLD, (i.e.  each time you open a new cmd.exe prompte. OR (in Windows 10), right click on This PC -> properties. Click on Advanced system settings -> Environment Variables (bottom right). Select Path and click Edit. Click new and enter C:\Program Files\nodejs. Reboot and you should be able to run node from any directory.

Run PHP Scripts from Node JS, Make a call to a PHP script from Node JS with the exec command. A bit of digging and all of the solutions I was able to find suggested running  This operation executes the myjsfile.js script in a mongo shell that connects to the test database on the mongod instance accessible via the localhost interface on port 27017. Alternately, you can specify the mongodb connection parameters inside of the javascript file using the Mongo() constructor.

  • May I know why the downvote and whats wrong with this question?
  • Does it work when running the command from CLI ?
  • Yeah it does run properly when executed from the terminal
  • 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
  • I am still getting the same error even after changing the directory in the exec command
  • you need to run npm install into the folder that contains your node project (the project with your package.json file)
  • 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
  • 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