Error: Could not find the include file "partials/head"
Asked Answered
S

6

1

enter image description hereI am trying to run an ejs file and gets error Error: Could not find the include file "partials/head".

i have checked most of the articles from stackoverflow and github however not able to resolve it ...

Error: Could not find the include file "partials/head" at getIncludePath (C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:162:13) enter code here at includeSource (C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:306:17) at C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:672:26 at Array.forEach () at Template.generateSource (C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:648:15) at Template.compile (C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:552:12) at Object.compile (C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:388:16) at handleCache (C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:212:18) at tryHandleCache (C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:251:16) at View.exports.renderFile [as engine] (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 file

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

expected actual result is .. when i run node server.js , it should display the index.ejs file

Sulphurate answered 17/2, 2019 at 8:14 Comment(1)
<!DOCTYPE html> <html language="en"> <head> <title>Welcome</title> <% 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> <% inlcude partials/footer.ejs %> </footer> </body> </html>Sulphurate
T
2

That's because your ejs file is in another directory which is views which mean that your partials directory is outside if your views directory..

Try this:

<%- include('../partials/head.ejs') %>
Trusting answered 3/1, 2021 at 1:15 Comment(0)
I
2

In 2021, October

I'm also having the same issue with importing files on ejs. But I used to figure it out the following way to include my other ejs partials from the parent directory.

Pros

  1. Don't need to set up view engine and directory path on server file.
  2. Independent directory names and locations we can achieve.

Working file: src/views/en/confirmation.ejs

Parent file: src/views/common/footer.ejs (Needs to included on each ejs file)

In 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>
Ixtle answered 12/10, 2021 at 6:27 Comment(1)
FYI for serverless users: this worked for me on serverless.yml project without using copy file plugin or anything elseKirit
I
1

Remove include tag header and footer from ejs tag in your index.ejs file. Or jsut include header and footer file properly. Like this

<%- include views-directory/filename  %>
Intumescence answered 17/2, 2019 at 8:24 Comment(5)
if i remove include it works fine ..but when i use include with the proper directory call it does not work. .Error: Could not find the include file "views/partials/head" at getIncludePath (C:\Users\Junia\Desktop\node\node_modules\ejs\lib\ejs.js:162:13) at includeSourceSulphurate
Where is your index.ejs file. Can you show me directory of view?Intumescence
Root Folder named ->node- views-pages-index.ejs and the server.js in the node folderSulphurate
Issue is resolved with this <%- include ../partials/head %> Thank youSulphurate
this is out the outdated answer and it's completely deprecated now. DON'T USE IT or you'll end up with an error.Ixtle
C
1

I got the same issue while implementing and after investing around 2-3 hr I figured out I was writing

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

instead of

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

Well, the only difference was the space between include and opening bracket.

Colophony answered 1/4, 2021 at 15:10 Comment(0)
P
-1

I also came across the same issue when I change the location of header and footer from views to its subfolder partials. And, in order to reach new header and footer location, I updated the path inside the string as below and it worked for me-

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

<!-- file path for footer.ejs -->

<%- include('./partials/footer'); -%>
Piety answered 6/7, 2023 at 8:10 Comment(0)
J
-2

This can happen, given the way you are calling the page you need, currently the correct way to get one page within another is

<%- include('partials/navigation.html')%>
Jaban answered 18/4, 2020 at 17:3 Comment(1)
ejs file only can include other ejs file not a html files.Ixtle

© 2022 - 2024 — McMap. All rights reserved.