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
|
Show 3 more comments
10 Answers
Reset to default 17Include 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") %>
- VS Code
- Left Side File Explorer
- Right Click on the '.ejs' file
- Click on 'Copy Path'
- 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
版权声明:本文标题:javascript - Could not find include include file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738102386a2063938.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
npm i ejs
? – zero298 Commented Sep 19, 2017 at 21:17npm install ejs
– the_prole Commented Sep 19, 2017 at 21:18home.ejs
in a directory called/views/
? – zero298 Commented Sep 19, 2017 at 21:18<% include partials/footer %>
– Keith Commented Sep 19, 2017 at 21:27