2015-06-07 109 views
1

的错误是:PHP-Node节点Express无法找到模块

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) 

的路径选择这个页面呈现为PHP如下:

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

我遇到一个NPM安装-g ,npm安装节点php -g在项目级别和以上级别都没有成功:

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], [email protected] 
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], [email protected] 
6.2, [email protected]) 
├── [email protected] ([email protected]) 
└── [email protected] ([email protected], [email protected], [email protected]) 

F:\Users\MichaelJacksonIsDead\publisherServer>npm start 

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

GET /index.php 500 41.145 ms - 954 

有没有人有这个问题expirence?至于我可以告诉大家我已经正确安装......也想确保我加入了包到我的JSON文件,如下所示:

"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" 
    } 

最后我没有足够的代表开个标签PHP节点该项目@https://www.npmjs.com/package/php-node,而不是一个问题,但会的是很高兴有一个更speficic标签:)

+0

它看起来并不像你遵循[说明](https://github.com/digplan/php-node),而是你需要路由中的中间件? – adeneo

回答

2

我已经找到了答案,谢谢@mscdex和@adeneo

我更换了默认:

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

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

下路线:

/* 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'); 
}); 

该应用程序仍然支持ph p和index.hjs页面。

1

你应该只有使用时您正在安装从NPM模块包含一个命令行实用程序-g(如express-generator就是这样的一个例子)。然而,在几乎所有情况下,您只需简单地执行npm install <module name>即可在本地安装模块,并允许您将其require()它。

所以npm install在你的项目的根目录中,require()应该能够找到php-node