admin管理员组

文章数量:1178545

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 Objectpile (/home/ubuntu/workspace/node_modules/ejs/lib/ejs.js:509:12)
    at Objectpile (/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>

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>
Share Improve this question edited Sep 19, 2017 at 21:24 the_prole asked Sep 19, 2017 at 21:14 the_prolethe_prole 8,94519 gold badges87 silver badges178 bronze badges 8
  • Have you run npm i ejs? – zero298 Commented Sep 19, 2017 at 21:17
  • @zero298 yes i ran npm install ejs – the_prole Commented Sep 19, 2017 at 21:18
  • Do you have a file called home.ejs in a directory called /views/? – zero298 Commented Sep 19, 2017 at 21:18
  • @zero298 yes I do – the_prole Commented Sep 19, 2017 at 21:19
  • 4 <% include partials/footer %> – Keith Commented Sep 19, 2017 at 21:27
 |  Show 3 more comments

10 Answers 10

Reset to default 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

For me I had to set root parameter,

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

And then use it like,

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

Try any of these:

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

Try this :

<% include header %>

    <h1>welcome</h1>

<% include footer%>

<%- 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.)

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}) %>
<% } %>

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'); -%>

Faced same issue,resolved with below

<%- include('header.ejs');-%>
   <h1>Home</h1>
   <p><%=startingContent%></p>
<%- include('footer.ejs');-%>

In your home.ejs file add this

  <%- include("../partials/header") %>
    <h1>Hi mom!</h1>
  <%- include("../partials/footer") %>
  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.

本文标签: javascriptCould not find include include fileStack Overflow