php-node Node Express 找不到模块

php-node Node Express cannot find module

错误是:

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 install -g, npm install node-php -g 在项目级别和以上级别都没有成功:

F:\Users\MichaelJacksonIsDead\publisherServer>npm install -g
publisherServer@0.0.0 F:\Users\MichaelJacksonIsDead\AppData\Roaming\npm\node_mod
ules\publisherServer
├── php-node@0.0.2
├── debug@2.2.0 (ms@0.7.1)
├── serve-favicon@2.2.1 (ms@0.7.1, fresh@0.2.4, parseurl@1.3.0, etag@1.6.0)
├── cookie-parser@1.3.5 (cookie@0.1.3, cookie-signature@1.0.6)
├── morgan@1.5.3 (basic-auth@1.0.1, depd@1.0.1, on-finished@2.2.1)
├── body-parser@1.12.4 (bytes@1.0.0, content-type@1.0.1, depd@1.0.1, raw-body@2.
0.2, on-finished@2.2.1, qs@2.4.2, iconv-lite@0.4.8, type-is@1.6.2)
├── express@4.12.4 (merge-descriptors@1.0.0, utils-merge@1.0.0, methods@1.1.1, f
resh@0.2.4, cookie@0.1.2, escape-html@1.0.1, cookie-signature@1.0.6, range-parse
r@1.0.2, content-type@1.0.1, parseurl@1.3.0, finalhandler@0.3.6, vary@1.0.0, ser
ve-static@1.9.3, content-disposition@0.5.0, path-to-regexp@0.1.3, depd@1.0.1, on
-finished@2.2.1, qs@2.4.2, etag@1.6.0, proxy-addr@1.0.8, send@0.12.3, type-is@1.
6.2, accepts@1.2.7)
├── hjs@0.0.6 (hogan.js@3.0.2)
└── less-middleware@1.0.4 (mkdirp@0.3.5, node.extend@1.0.10, less@1.7.5)

F:\Users\MichaelJacksonIsDead\publisherServer>npm start

> publisherServer@0.0.0 start F:\Users\MichaelJacksonIsDead\publisherServer
> node ./bin/www

GET /index.php 500 41.145 ms - 954

有人对这个问题有过期吗?据我所知,我已经正确安装了它......还要确保我将软件包添加到我的 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-node @ https://www.npmjs.com/package/php-node,这不是问题,但如果有一个更具体的标签会更好:)

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

在你的项目的根目录中npm install也是如此,require()应该能够找到php-node

我找到了答案,谢谢@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');
});

该应用仍支持 php 和 index.hjs 个页面。

只有当插件被使用时你不是 100% 满意它有一个简单的方法(这只是一个原型所以如果你是 node.js 专家你可以改进它)只从一个构建服务器应用程序 NODE.JS - PHP(理论上你还没有 sqlite3,在安装 mariadb 或 mysql 服务器后你也可以使用它们):