Home > other >  How do I generate an html file with ejs in command line?
How do I generate an html file with ejs in command line?

Time:01-15

I'm learning an ejs tutorial, which gives this command

ejs ./template_file.ejs -f data_file.json -o ./output.html

to generate an html file in command line.

Here is what I did on my ubuntu 20.04 LTS.

I created a directory

mkdir ejs-demo && cd "$_"

I initialized a new npm project

npm init -y

I installed the ejs package:

npm install ejs

I created a new file named template_file.ejs

nano template_file.ejs

I put the code from another tutorial in template_file.ejs

OK, so have fun! :D
-------------------
<%
    var fruits = ["Apple", "Pear", "Orange", "Lemon"]
      , random = " ".repeat(2).split("").map(x => Math.random())
      ;
%>

These fruits are amazing:
<% for(var i = 0; i < fruits.length;   i) {%>
  - <%=fruits[i]%>s<% } %>

Let's see some random numbers:

<% random.forEach((c, i) => {
%> <%=c.toFixed(10)   ((i   1) % 6 === 0 ? "\n": "") %><%});%>

I saved and quit the editor.

I tried the following command

ejs ./template_file.ejs -o ./output.html

and got

Command 'ejs' not found, did you mean

What am I missing?

CodePudding user response:

This is because ejs executable is not in your $PATH but in node_modules/.bin directory and shell couldn't find it.

Use npm'x exec command to run binaries provided with packages:

npm exec -- ejs ./template_file.ejs -o ./output.html

Or with an alias:

npx ejs ./template_file.ejs -o ./output.html
  •  Tags:  
  • Related