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

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

我正在学习一个 ejs tutorial,它给出了这个命令

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

在命令行中生成一个html文件。

这是我在 ubuntu 20.04 LTS 上所做的。

我创建了一个目录

mkdir ejs-demo && cd "$_"

我初始化了一个新的 npm 项目

npm init -y

我安装了 ejs 软件包:

npm install ejs

我创建了一个名为 template_file.ejs

的新文件
nano template_file.ejs

我把 another tutorial 的代码放在 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": "") %><%});%>

我保存并退出了编辑器。

我尝试了以下命令

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

得到了

Command 'ejs' not found, did you mean

我错过了什么?

这是因为 ejs 可执行文件不在您的 $PATH 中,而是在 node_modules/.bin 目录中并且 shell 无法找到它。

使用 npm'x exec command 到 运行 软件包提供的二进制文件:

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

或使用别名:

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