Executing nodejs script file in PHP using exec()
how to include php file in node js
node js in 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
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
$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 (  => module.js:457  => throw err;  => ^  =>  => Error: Cannot find module 'some_module'  => at Function.Module._resolveFilename (module.js:455:15)  => at Function.Module._load (module.js:403:25)  => at Module.require (module.js:483:17)  => at require (internal/module.js:20:19)  => at Object.<anonymous> (/var/www/html/projectfolder/js/nodefunc.js:5:9)  => at Module._compile (module.js:556:32)  => at Object.Module._extensions..js (module.js:565:10)  => at Module.load (module.js:473:32)  => at tryModuleLoad (module.js:432:12)  => at Function.Module._load (module.js:424:3) )
I got it finally. Its just ignorng the
NODE_PATH variable for reasons unkown :(
Nodejs File I hade to give the absolute path of the module like this:
var request = require("/usr/lib/node_modules/request");
- check whether the modules are installed (
- 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
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.
- 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 installbecause I can see those installed modules folder in this path
- I am still getting the same error even after changing the directory in the
- 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