writing ejs in a script is giving me the error 'Expression expected'

Viewed 1837

I have a script tag in my show.ejs file, and I have the following line:

<script>
    const post = <%- JSON.stringify(post) %>
</script>

I'm getting the error of 'Error Expected' on both the opening and closing ejs tags. I was wondering why this error occurs and if there was anything I'm doing that isn't allowed.

2 Answers

I just have same issue with VS code if you using VS you need to add to end of settings.json in cofiguration of html this line

"html.validate.scripts": false,

find html in bottom right find html in bottom right

then find configure HTML language then find configure HTML language and add line to end of json file and add line to end of json file

There are 2 optional ways.

1)

Change the global delimiters of EJS.

const ejs = require("ejs");

ejs.delimiter = '/';
ejs.openDelimiter = '[';
ejs.closeDelimiter = ']';

Update your script.

<script>
const post= [/- JSON.stringify(post) /];
</script>

2)

Add this property to settings.json in VSCode.

"html.validate.scripts": false

Change the global delimiters of EJS.

const ejs = require("ejs");

ejs.delimiter = '?';
ejs.openDelimiter = '[';
ejs.closeDelimiter = ']';

Update your script.

<script>
const post= [?- JSON.stringify(post) ?];
</script>

Note: Auto format will work correctly in 2 ways.

Note: I prefer the first one because I can use both auto format and scripts validation and I don't need any EJS extension.

Related