Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "SyntaxError: Unexpected token '/' in...index.ejs while compiling ejs" error after updating npm packages

I just updated EJS from 2.7.4 to 3.1.5 and some other packages in my project. Upon doing it, I can't access any of the webpages but getting an error instead. Apparently all of the pages throws the same error. Besides that I get VS Code errors in some of the code in my ejs template which didn't matter before. I mean the errors were there before I updated all the npm packages and the app worked perfectly fine.

This is the endpoint:

router.get("/", async (req, res) => {

    try{
        const url           = `http://api.example.co/?key=${process.env.API_KEY}`;
        const fullData      = await axios.get(url);
        
        res.render("../views/index", {data: fullData});
    }catch(err) {
        res.render("../views/index", {data: null});
    }
    
});

This is the error: enter image description here

This is how I get data in my ejs template:

<% if(data){ %> 
    Name <%= data.name %>        
<% } %>

This is the code in my script tags in my ejs template which shows red underline: enter image description here

<script type="text/javascript" >
    <% if(data) { %>
    var map = L.map('map').setView([<%= data.latitude %>, <%= data.longitude %>], 11);
    var marker = L.marker([<%= data.latitude %>, <%= data.longitude %>]).addTo(map);

    L.tileLayer('https://api.maptiler.com/maps/streets/{z}/{x}/{y}.png?key=boomBoomBoomm', {
    attribution: '<a href="https://www.maptiler.com/copyright/" target="_blank">&copy; MapTiler</a> <a href="https://www.openstreetmap.org/copyright" target="_blank">&copy; OpenStreetMap contributors</a>'
}).addTo(map);
    <% } %>
</script>

What could be the issue? Am I doing something wrong? Is the update of something clashing with something else? Please help!

like image 989
Zak Avatar asked Dec 05 '25 18:12

Zak


1 Answers

Found the problem. Apparently the newer versions of EJS requires including partials of header and footer in a different way than before.

So changing this:

<% include ./partials/header %>

to

<%- include ("./partials/footer") %>

fixed the issue.

like image 136
Zak Avatar answered Dec 08 '25 06:12

Zak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!