【问题标题】:Could not find include include file找不到包含包含文件
【发布时间】:2018-02-28 19:03:22
【问题描述】:

我正在运行一个简单的服务器

var express = require('express')
var app = express()

app.set('view engine', 'ejs'); 
app.use(express.static('public')) 

// home page request handler
app.get('/', function (req, res) {

    res.render('home')
})

// initializes request listener
app.listen(process.env.PORT, process.env.IP, function(){
    console.log("Server is listening");
})

当我对主页发出 GET 请求时,运行时会抛出以下错误

Error: Could not find include include file.
    at getIncludePath (/home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:152:13)
    at includeSource (/home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:276:17)
    at /home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:629:26
    at Array.forEach (native)
    at Object.generateSource (/home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:605:15)
    at Object.compile (/home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:509:12)
    at Object.compile (/home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:358:16)
    at handleCache (/home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:201:18)
    at tryHandleCache (/home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:223:14)
    at View.exports.renderFile [as engine] (/home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:437:10)

我不明白这个错误。有任何想法吗?我在 Cloud9 工作。

我的目录结构是

v1.1
  +---views
  |     +---- home.ejs
  |     +---- partials
  |               +------ header.ejs
  |               +------ footer.ejs
  |
  +----app.js

home.ejs

<% include header %>
<h1>welcome</h1>
<% include footer %>

header.ejs

<DOCTYPE! html>
    <html>
        <head>
            <title>
                <link rel="stylesheet" hreff="app.css">
            </title>
        </head>
    <body>

footer.ejs

    </body
</html>

【问题讨论】:

  • 你跑npm i ejs了吗?
  • @zero298 是的,我跑了npm install ejs
  • /views/ 的目录中是否有一个名为home.ejs 的文件?
  • @zero298 是的,我愿意
  • &lt;% include partials/footer %&gt;

标签: javascript node.js ejs cloud9


【解决方案1】:

包含路径是相对的,您需要更新路径以包含“partials”子文件夹,例如

<% include partials/header %>
<h1>welcome</h1>
<% include partials/footer %>

docs

【讨论】:

    【解决方案2】:

    尝试以下任何一种:

    <% include header.ejs %>
    <% include header %>
    <%- include('header.ejs'); -%>
    <%- include('./header.ejs'); -%>
    

    【讨论】:

      【解决方案3】:

      试试这个:

      <% include header %>
      
          <h1>welcome</h1>
      
      <% include footer%>
      

      【讨论】:

        【解决方案4】:
        1. VS 代码
        2. 左侧文件资源管理器
        3. 右键单击“.ejs”文件
        4. 点击“复制路径”
        5. 然后粘贴该路径
        <%- include('YOUR_PATH/GOES_HERE') %>
        

        就我而言, OS Ubuntu,Path 是这样的

        <%- include('/media/username/diskname/foldername/nodejsApp/views/body/header.ejs') %>
        

        编辑

        我在写答案时忘记了“%”。

        【讨论】:

          【解决方案5】:

          先决条件:确保文件名与上面声明的名称相关。即(标题不等于标题。)

          【讨论】:

            【解决方案6】:

            对我来说,我必须设置 root 参数,

            ejs.render(
              html,
              {},
              {
                root: process.cwd(),
              }
            )
            

            然后像这样使用它,

            <%- include('/footer/index.ejs'); %>
            

            【讨论】:

              猜你喜欢
              • 2018-06-04
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-11-25
              • 2013-03-05
              • 2011-10-22
              • 2012-11-01
              • 1970-01-01
              相关资源
              最近更新 更多