【问题标题】:EJS module not found on OpenShift NodeJS template在 OpenShift NodeJS 模板上找不到 EJS 模块
【发布时间】:2013-02-19 12:07:06
【问题描述】:

我无法在 NodeJS 上使用 EJS 作为我的视图渲染引擎。

我发现了几个类似的问题,但都说明了安装真正适用于其他人的 EJS。这对我来说不一样,可能是因为我无法为安装选择正确的目录(在 OpenShift 存储库中的许多重复项中)。

我有 OpenShift 模板创建的 NodeJS 默认应用程序。在安装 EJS 依赖项时,我以某种方式搞砸了,我收到以下错误(不可用模块的标准 NodeJS 错误):

错误:找不到模块“ejs” 在 Function._resolveFilename (module.js:337:11) 在 Function._load (module.js:279:25) 在 Module.require (module.js:359:17) 在需要(module.js:375:17) 在 View.templateEngine (/usr/lib/node_modules/express/lib/view/view.js:134:38) 在 Function.compile (/usr/lib/node_modules/express/lib/view.js:68:17) 在 ServerResponse._render (/usr/lib/node_modules/express/lib/view.js:417:18) 在 ServerResponse.render (/usr/lib/node_modules/express/lib/view.js:318:17) 在 /var/lib/openshift/5123c2494382ec16ca000222/app-root/runtime/repo/server.js:114:17 在回调(/usr/lib/node_modules/express/lib/router/index.js:272:11)

除了在 package.json 中提到之外,我还尝试通过终端在 app-root、runtime 和 nodejs-0.6 级别安装 ejs(并重新启动应用程序),但没有用。

我的目录结构是:

-应用程序根 - -数据 ---回购 -----node_modules(有ejs) -----server.js -----package.json ("依赖": {"ejs" : ">=0.8.3"},) -----观看次数 --------defaultError.ejs - -运行 - - -数据 -----node_modules(空) -----repo(与 app-root/repo 相同) -------node_modules(有ejs) -nodejs-0.6 - -数据 ---repo(与 app-root/repo 相同) -----node_modules(有ejs) - -运行 -----node_modules(空) -----回购(相同) -------node_modules(有ejs)

defaultError.ejs 只是普通的 html。 server.js 有以下内容:

self.createRoutes = function() {
        self.routes = { };

        //...
        self.routes['/'] = function(req, res) {
            res.setHeader('Content-Type', 'text/html');
            res.send(self.cache_get('index.html') );
        };

        self.routes['/helloejs'] = function(req, res){
            res.render('defaultError', { layout:false } );
        };
    };

self.initializeServer = function() {
        self.createRoutes();
        self.app = express.createServer();

        self.app.set('view engine', 'ejs');
        //  Add handlers for the app (from the routes).
        for (var r in self.routes) {
            self.app.get(r, self.routes[r]);
        }
    };

希望这篇长篇文章能说明我的问题:)

【问题讨论】:

    标签: node.js ejs openshift


    【解决方案1】:

    错误是由于使用了不正确的目录。您必须从项目目录安装软件包并运行您的应用程序。这是我用作项目的文件夹的目录结构。

    .
    ├── app.js
    ├── node_modules
    │   ├── express
    │   ├── jade
    │   └── socket.io
    ├── package.json
    ├── public
    │   ├── images
    │   ├── javascripts
    │   └── stylesheets
    ├── routes
    │   ├── index.js
    ├── server.js
    └── views
        └── layout.jade
    

    使用此文件夹中的npm install 在 node_modules 子文件夹中添加包文件夹(如expresssocket.io)。你应该在这个位置使用node,因为所有本地安装的模块都只能在这个文件夹中看到。

    【讨论】:

    • 我提到的目录结构由 OpenShift 平台维护,因此它可以以标准方式管理构建/部署周期。如我所见,该结构也与您的结构相似。虽然这里有很多重复的“repo/”目录,但我迷路了。我还尝试安装 ejs 目录“app-root/runtime/”和“app-root/runtime/repo”(原始错误跟踪中提到的目录)。
    • 我不确定确切的原因,但这就是我的问题消失的方式:我在 package.json 中添加了“express”。这很奇怪,因为默认安装 express 可用于 OpenShift 上的所有应用程序。将上述答案标记为正确,因为它至少提供了一个必要的检查点。
    【解决方案2】:

    我的情况,我只是在 package.json 中手动添加了 ejs:

     {
       "name": "myApp"
       "dependencies": {
         "express": "^4.12.2",
         "ejs": "^1.0.0"
       }
     }
    

    然后运行 ​​npm install(也许你需要用 sudo 运行它) 请注意,默认情况下,ejs 会查看视图目录

    【讨论】:

      【解决方案3】:

      安装 ejs 时必须使用选项 --save:

      npm install ejs --save
      

      并创建 .gitignore 文件:

      node_modules
      

      【讨论】:

        猜你喜欢
        • 2017-10-18
        • 1970-01-01
        • 2011-12-06
        • 1970-01-01
        • 2017-12-16
        • 2014-01-29
        • 1970-01-01
        • 2013-12-25
        • 2016-03-15
        相关资源
        最近更新 更多