【问题标题】:Error: Could not find the include file "partials/head"错误:找不到包含文件“partials/head”
【发布时间】:2021-10-12 14:27:05
【问题描述】:

enter image description here我正在尝试运行 ejs 文件并收到错误错误:找不到包含文件“partials/head”。

我已经检查了来自 stackoverflow 和 github 的大部分文章,但无法解决它...

错误:找不到包含文件“partials/head” 在 getIncludePath (C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:162:13) 输入 includeSource 处的代码 (C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:306:17) 在 C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:672:26 在 Array.forEach () 在 Template.generateSource (C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:648:15) 在 Template.compile (C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:552:12) 在 Object.compile (C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:388:16) 在 handleCache (C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:212:18) 在 tryHandleCache (C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:251:16) 在 View.exports.renderFile [作为引擎] (C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:480:10)

var express=require('express');

var app=express();

var router=express.Router();

var mysql=require('mysql');

var cookieParser=require('cookie-parser');

var session=require('express-session');

app.use(session({
    secret: 'secret',
    resave: true,
    saveUninitialized: true
    //cookie : { maxAge : 60000 }
}));

var path=require('path');

var bodyParser=require('body-parser');

app.use(express.static('/'));

//Serves all the request which includes /images in the url from Images folder

app.use('/images', express.static(__dirname + '/images'));

app.use('/bs4', express.static(__dirname + '/bs4'));

app.use(cookieParser());

const ejsLint = require('ejs-lint');

var con=mysql.createConnection(

{

host:'localhost',

user:'root',

password:'',

database:'shintoj'

});

var path = require('path');

app.use('/',router);

app.use('/',express.static(__dirname + '/'));

app.set('views', path.join(__dirname, 'views/pages'));

app.set('view engine', 'ejs');// use res.render to load up an ejs view file

// index page 

router.get('/', function(req, res) {

    //res.send('Welcome');

    res.render('index');

});

app.listen(8080);

console.log('8080 is the magic port');

console.log(app.get('views'));

index.ejs 文件

<head>

<%- include partials/head %>

</head>

<body class="container">

<header>

<% include partials/header.ejs %>

</header>

    <div class="jumbotron">

        <h2>Welcome to our services .</h2>

        <p>Hello</p>

    </div>

<footer>

<% include partials/footer.ejs %>

</footer>   

预期的实际结果是.. 当我运行 node server.js 时,它应该显示 index.ejs 文件

【问题讨论】:

  • 欢迎

    欢迎使用我们的服务。

    你好

标签: node.js ejs


【解决方案1】:

那是因为您的 ejs 文件位于另一个目录中,即 views 这意味着如果您的视图目录在您的部分目录之外..

试试这个:

<%- include('../partials/head.ejs') %>

【讨论】:

    【解决方案2】:

    我在实施时遇到了同样的问题,在投入了大约 2-3 小时后,我发现我正在写作

    <%- include ('partials/header') %> 
    

    而不是

    <%- include('partials/header') %> 
    

    嗯,唯一的区别是包含和左括号之间的空格。

    【讨论】:

      【解决方案3】:

      从 index.ejs 文件中的 ejs 标记中删除包含标记的页眉和页脚。 或者 jsut 正确包含页眉和页脚文件。像这样

      <%- include views-directory/filename  %>
      

      【讨论】:

      • 如果我删除包含它工作正常..但是当我使用包含正确的目录调用它不起作用。 .Error:在 includeSource 的 getIncludePath (C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:162:13) 处找不到包含文件“views/partials/head”
      • 你的 index.ejs 文件在哪里。你能告诉我视图目录吗?
      • 根文件夹名为 ->node-views-pages-index.ejs 和 node 文件夹中的 server.js
      • 这个问题解决了 谢谢
      • 这是过时的答案,现在已完全弃用。 不要使用它,否则你会遇到错误。
      【解决方案4】:

      2021 年 10 月

      我在ejs 上导入文件时也遇到了同样的问题。但我曾经想出以下方法来包含父目录中的其他 ejs 部分。

      优点

      1. 不需要在服务器文件上设置view engine and directory path
      2. 我们可以实现的独立目录名称和位置。

      工作文件:src/views/en/confirmation.ejs

      父文件:src/views/common/footer.ejs(需要包含在每个 ejs 文件中)

      在 src/views/en/confirmation.ejs`中

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      <html data-editor-version="2" class="sg-campaigns" xmlns="http://www.w3.org/1999/xhtml">
           <body>
               <h1>this is body</h1>
           </body>
           
           <% var dir = process.cwd() %>
           <%- include(dir + "/src/views/common/footer.ejs") %>
        
      </html>
      

      【讨论】:

        【解决方案5】:

        这可能发生,考虑到您调用所需页面的方式,目前在另一个页面中获取一个页面的正确方法是

        <%- include('partials/navigation.html')%>
        

        【讨论】:

        • ejs 文件只能包含其他ejs 文件而不是html 文件。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-29
        • 1970-01-01
        • 2023-03-24
        • 2019-09-08
        • 1970-01-01
        • 1970-01-01
        • 2018-02-28
        相关资源
        最近更新 更多