【问题标题】:php-node Node Express cannot find modulephp-node Node Express 找不到模块
【发布时间】:2015-06-07 13:36:07
【问题描述】:

错误是:

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的Route如下:

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

最后,我没有足够的代表来为项目@https://www.npmjs.com/package/php-node 打开标签 php-node,这不是问题,但如果有一个更具体的标签会很好:)

【问题讨论】:

  • 看起来您没有遵循instructions,而是您需要路由中的中间件?

标签: php node.js express php-node


【解决方案1】:

我已经找到答案了,谢谢@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 页面。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      只有在使用插件的情况下,您才不会 100% 满意,有一个简单的方法(这只是一个原型,所以如果您是 node.js 专家,您可以改进它)仅从一个服务器应用程序节点构建。 JS - PHP(理论上你还没有 sqlite3,在安装 mariadb 或 mysql 服务器后你也可以使用它们): https://stackoverflow.com/a/68422021/5781320

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-16
        • 1970-01-01
        • 2017-04-16
        • 1970-01-01
        • 2012-11-20
        • 2014-02-21
        • 2015-08-01
        • 1970-01-01
        相关资源
        最近更新 更多