EJS编译报错Unexpected token ==的问题咨询
Hey there, let's break down why you're hitting that EJS compilation error and how to fix it quickly.
The core issue is a syntax mistake in how you're passing parameters to your EJS partial. When using include in EJS, you need to pass a valid JavaScript object literal—key-value pairs are separated with a colon :, not the equality operator ==. Your code { someVariable == "style" } isn't valid object syntax, so the EJS compiler throws an error when it encounters that unexpected ==.
1. Correct the Parameter Passing Syntax
Update your include statement in the main file to use a colon instead of == to define the parameter:
<%- include('partials/nameofFile.ejs', { someVariable: "style" }) %>
This creates a proper object where the key someVariable maps to the value "style", which EJS can parse without issues.
2. Clean Up Your Partial's Logic Structure
You also have an unnecessary EJS tag split around your else if in the partial, which could lead to other syntax hiccups. Rewrite the partial to keep the conditional logic in a single continuous block:
<% if(someVariable === "style"){ %> .some-css{ bottom: -100px; visibility: visible; } <% } else if(someVariable === "code") { %> <div class="some-css" > some code </div> <% } %>
3. Bonus: Use Strict Equality for Safer Checks
As a best practice, swap out == with === in your conditionals. === checks both value and type, preventing unexpected behavior from implicit type conversion (like comparing a string to a number by accident).
内容的提问来源于stack exchange,提问作者tellme Oberlin

