2015-06-07 38 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