Could not find include include file
Asked Answered
S

10

11

I'm running a simple server

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

When I make a GET request for the home page, run-time throws the following error

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)

I don't understand this error. Any ideas? I'm working in Cloud9.

My directory structure is

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>
Stringed answered 19/9, 2017 at 21:14 Comment(8)
Have you run npm i ejs?Tricornered
@Tricornered yes i ran npm install ejsStringed
Do you have a file called home.ejs in a directory called /views/?Tricornered
@Tricornered yes I doStringed
What does home.ejs look like? Error suggests an issue including a partial viewDepot
@Depot I added the file you asked forStringed
<% include partials/footer %>Coroner
No probs, must say ejs error is not very helpful.. The include file might have helped.. :) Also the other guys helped here, getting you to show more details.Coroner
D
17

Include paths are relative, you will need to update your paths to include the "partials" subfolder e.g.

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

See the docs

Depot answered 19/9, 2017 at 21:32 Comment(0)
P
3

For me I had to set root parameter,

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

And then use it like,

<%- include('/footer/index.ejs'); %>
Peery answered 2/12, 2021 at 6:1 Comment(0)
E
1

Try any of these:

<% include header.ejs %>
<% include header %>
<%- include('header.ejs'); -%>
<%- include('./header.ejs'); -%>
Erg answered 29/10, 2020 at 1:58 Comment(0)
C
0

Try this :

<% include header %>

    <h1>welcome</h1>

<% include footer%>
Caudle answered 29/6, 2020 at 21:15 Comment(0)
K
0

<%- include ('Filename without adding .ejs') %>

prerequisite: Ensure the filename correlates with the declared name in the above. i.e (Header is not equal to header.)

Ked answered 22/8, 2021 at 16:42 Comment(0)
A
0

Don't put the extension after the filename. For example, do this:

include("file", { item: 123})

...not this:

include("file.ejs", { item: 123})

If you have multiple includes in a loop, don't put the whole loop in one <% %> block. Put the beginning and end in their own <% %> and the includes in a <%- %> tag. It won't work with just a <% %> tag, you need to use <%- %>:

<% for(const item of items) { %>
    <%- include("file", { item: 123}) %>
<% } %>
Amylase answered 8/4, 2022 at 22:59 Comment(0)
C
0

I also faced issues with this, but I add file extension, and it did work for me. Try the following way:

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

<%- include('footer.ejs'); -%>
Chinese answered 3/7, 2022 at 4:6 Comment(0)
E
0

Faced same issue,resolved with below

<%- include('header.ejs');-%>
   <h1>Home</h1>
   <p><%=startingContent%></p>
<%- include('footer.ejs');-%>
Enate answered 27/4, 2023 at 13:17 Comment(0)
E
0

In your home.ejs file add this

  <%- include("../partials/header") %>
    <h1>Hi mom!</h1>
  <%- include("../partials/footer") %>
Ectopia answered 16/7, 2023 at 15:21 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Piperine
A
-1
  1. VS Code
  2. Left Side File Explorer
  3. Right Click on the '.ejs' file
  4. Click on 'Copy Path'
  5. Then paste that path
<%- include('YOUR_PATH/GOES_HERE') %>

In my Case, OS Ubuntu, Path was like this

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

EDIT

I forget '%' while writing answer.

Alister answered 15/7, 2021 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.