2

The error is:

Cannot find module 'php'

Error: Cannot find module 'php'
    at Function.Module._resolveFilename (module.js:336:15)
    at Function.Module._load (module.js:278:25)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at new View (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\view.js:50:49)
    at EventEmitter.app.render (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\application.js:545:12)
    at ServerResponse.res.render (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\response.js:938:7)
    at F:\Users\MichaelJacksonIsDead\publisherServer\routes\index.js:12:6
    at Layer.handle [as handle_request] (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\router\layer.js:82:5)
    at next (F:\Users\MichaelJacksonIsDead\publisherServer\node_modules\express\lib\router\route.js:110:13)

The Route for this page to render as php is as follows:

router.get('/index.php', function(req, res, next) {
    var render = require('php-node')({bin:"F:\\xampp\php\\php.exe"});
    res.render('index.php');
});

I have run a npm install -g, npm install node-php -g at both project level and level above with no success:

F:\Users\MichaelJacksonIsDead\publisherServer>npm install -g
[email protected] F:\Users\MichaelJacksonIsDead\AppData\Roaming\npm\node_mod
ules\publisherServer
├── [email protected]
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], raw-body@2.
0.2, [email protected], [email protected], [email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], f
[email protected], [email protected], [email protected], [email protected], range-parse
[email protected], [email protected], [email protected], [email protected], [email protected], ser
[email protected], [email protected], [email protected], [email protected], on
[email protected], [email protected], [email protected], [email protected], [email protected], type-is@1.
6.2, [email protected])
├── [email protected] ([email protected])
└── [email protected] ([email protected], [email protected], [email protected])

F:\Users\MichaelJacksonIsDead\publisherServer>npm start

> [email protected] start F:\Users\MichaelJacksonIsDead\publisherServer
> node ./bin/www

GET /index.php 500 41.145 ms - 954

Does anyone have any expirence with this issue? As far as I can tell I have correctly installed it... also to make sure I added the package to my json file as follows:

  "dependencies": {
    "body-parser": "~1.12.4",
    "cookie-parser": "~1.3.5",
    "debug": "~2.2.0",
    "express": "~4.12.4",
    "hjs": "~0.0.6",
    "less-middleware": "1.0.x",
    "morgan": "~1.5.3",
    "serve-favicon": "~2.2.1",
    "php-node": "0.0.2"
  }

Lastly I did not have enough rep to open a tag php-node for the project @ https://www.npmjs.com/package/php-node, not a issue but would of been nice to have a more speficic tag :)

ninjaPixel
  • 6,122
  • 3
  • 36
  • 47
John
  • 1,309
  • 2
  • 12
  • 24
  • It doesn't look like you followed the [instructions](https://github.com/digplan/php-node), but instead you required the middleware inside the route ? – adeneo Jun 07 '15 at 13:38

3 Answers3

3

I have found the answer, thank you @mscdex and @adeneo

I replaced the default:

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hjs');

with

// use PHP as view engine in Express
var phpnode = require('php-node')({bin:"F:\\xampp\\php\\php.exe"});
app.engine('php', phpnode);
app.set('view engine', 'php');

under routes:

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index.hjs', { title: 'Express' });
});


router.get('/index.php', function(req, res, next) {
    res.render('index.php');
});

The app still supports php and index.hjs pages.

John
  • 1,309
  • 2
  • 12
  • 24
2

You should only use -g when you're installing a module from npm that contains a command line utility (e.g. express-generator is an example of this). However in almost all cases you should just simply do npm install <module name> which will install the module locally and allow you to require() it.

So do npm install in your project's root directory and require() should be able to find php-node.

mscdex
  • 104,356
  • 15
  • 192
  • 153
0

only if the plugin are use you are not 100% happy of it there is a simple method(this is only a prototype so if you are node.js expert you may improve it) to build FROM ONLY ONE SERVER APPS NODE.JS - PHP (and theoretical you have yet sqlite3 and after installing a mariadb or mysql server you can use them as well) : https://stackoverflow.com/a/68422021/5781320