编译register.ejs时出现SyntaxError: Unexpected identifier 'partials'错误求助
EJS编译报错SyntaxError: Unexpected identifier 'partials'的解决办法
开发新手按GitHub项目的README文档安装运行项目,编译register.ejs文件时持续出现SyntaxError: Unexpected identifier 'partials'错误。尝试注释代码不同部分甚至全部代码,错误依然存在,且已确认渲染时传入的模板路径正确。
错误日志
0|index | SyntaxError: Unexpected identifier 'partials' in /home/server/assets/views/register.ejs while compiling ejs 0|index | 0|index | If the above error is not helpful, you may want to try EJS-Lint: 0|index | https://github.com/RyanZim/EJS-Lint 0|index | Or, if you meant to create an async function, pass `async: true` as an option. 0|index | at new Function (<anonymous>) 0|index | at Template.compile (/home/server/node_modules/ejs/lib/ejs.js:673:12) 0|index | at Object.compile (/home/server/node_modules/ejs/lib/ejs.js:398:16) 0|index | at handleCache (/home/server/node_modules/ejs/lib/ejs.js:235:18) 0|index | at tryHandleCache (/home/server/node_modules/ejs/lib/ejs.js:274:16) 0|index | at exports.renderFile [as engine] (/home/server/node_modules/ejs/lib/ejs.js:491:10) 0|index | at View.render (/home/server/node_modules/express/lib/view.js:135:8) 0|index | at tryRender (/home/server/node_modules/express/lib/application.js:657:10) 0|index | at Function.render (/home/server/node_modules/express/lib/application.js:609:3) 0|index | at ServerResponse.render (/home/server/node_modules/express/lib/response.js:1039:7)
register.ejs代码
<!DOCTYPE html> <html> <% include partials/head.ejs %> <body> <style type="text/css"> body { background: #dbdbdb; background: -webkit-linear-gradient(to bottom right, #86fde8, #acb6e5); background: linear-gradient(to bottom right, #86fde8, #acb6e5); } body>.grid { height: 100%; } .image { margin-top: -100px; } .column { max-width: 450px; } </style> <script> $(document).ready(function () { $('.ui.form').form({ fields: { email: { identifier: 'username', rules: [ { type: 'empty', prompt: 'Please enter your username' } ] }, password: { identifier: 'password', rules: [ { type: 'empty', prompt: 'Please enter your password' } ] } } }); }); </script> <div class="ui middle aligned center aligned grid"> <div class="column"> <form class="ui large form" method="POST"> <div class="ui stacked segment"> <h2 class="ui image header"> <div class="content"> <img src="/logo.png" style="height: 70px;"> </div> </h2> <div class="field"> <div class="ui left icon input"> <i class="user icon"></i> <input type="text" name="username" placeholder="Username"> </div> </div> <div class="field"> <div class="ui left icon input"> <i class="lock icon"></i> <input type="password" name="password" placeholder="Password"> </div> </div> <div class="ui fluid large teal submit button">Register</div> <div class="ui error message"></div> </div> </form> <% include partials/footer.ejs %> </div> </div> </body> </html>
解决方案
这个报错是EJS版本语法不兼容导致的:
- 旧版EJS(v2及以下)使用
<% include partials/head.ejs %>这种语法引入模板 - 新版EJS(v3+)要求使用
<%- include('partials/head.ejs') %>的语法,必须用括号包裹路径,且用<%-输出内容
修改register.ejs里的两处引入代码:
- 将
<% include partials/head.ejs %>改为<%- include('partials/head.ejs') %> - 将
<% include partials/footer.ejs %>改为<%- include('partials/footer.ejs') %>
修改后重新编译即可解决该错误。
内容的提问来源于stack exchange,提问作者Ashmit Garg
相关产品推荐
相关产品推荐

