Toni Florea
Toni Florea

Reputation: 43

How to run node.js file from PHP file

so I've been trying to run an SMS script after the user submits a form. If I use

node send-sms.js 

locally on my machine, it works. However, if I try to use

$sendSmsPath = "send-sms.js";
exec('/public_html/node_modules/node '.$sendSmsPath);

Nothing happens. No error, nothing. I tried using a relative path, as well as an absolute one. My folder scheme is as follows: public > other_folder > php > php_file.php AND public > node_modules

Any ideas are welcome, thanks

Upvotes: 0

Views: 4684

Answers (2)

yawar khan
yawar khan

Reputation: 1

return or print your values in your js file so you will get these variabales in your output

$output = shell_exec('node C:\xampp\htdocs\noah-imports\app\NodeJs\index.js');

Upvotes: 0

user7885981
user7885981

Reputation:

You are using the wrong path to Node.js. Try this:

<?php
$sendSmsPath = "send-sms.js";
exec('/c/Program Files/nodejs/node '.$sendSmsPath);

node_modules is for the dependencies of your script, not the actual Node.js executable.

If you'd like to run your script on another machine make sure it has Node.js installed globally and you know the path of the executable (can be found with which node)

Upvotes: 2

Related Questions