简体   繁体   English

如何在命令行中使用 ejs 生成 html 文件?

[英]How do I generate an html file with ejs in command line?

I'm learning an ejs tutorial , which gives this command我正在学习一个ejs 教程,它给出了这个命令

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

to generate an html file in command line.在命令行中生成 html 文件。

Here is what I did on my ubuntu 20.04 LTS.这是我在 ubuntu 20.04 LTS 上所做的。

I created a directory我创建了一个目录

mkdir ejs-demo && cd "$_"

I initialized a new npm project我初始化了一个新的npm项目

npm init -y

I installed the ejs package:我安装了ejs package:

npm install ejs

I created a new file named template_file.ejs我创建了一个名为template_file.ejs的新文件

nano template_file.ejs

I put the code from another tutorial in template_file.ejs我将另一个教程中的代码放在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找不到命令“ejs”,你是说

What am I missing?我错过了什么?

This is because ejs executable is not in your $PATH but in node_modules/.bin directory and shell couldn't find it.这是因为ejs可执行文件不在您的 $PATH 中,而是在node_modules/.bin目录中,并且 shell 找不到它。

Use npm'x exec command to run binaries provided with packages:使用 npm'x exec 命令运行包提供的二进制文件:

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

Or with an alias:或使用别名:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM