LoginSignup
4
0

More than 1 year has passed since last update.

📗【数秒で簡単】ejsでもルート相対パスでインクルードする方法

Last updated at Posted at 2020-08-24

process.cwd()

Node.jsのprocess.cwd()は、コマンドが実行されたディレクトリを返します。

たとえば、次のようなファイル構成だったとしましょう。

./project
└ src/
 └ *.ejs
└ package.json
└ gulpfile.js

この場合、projectディレクトリでgulpコマンドをたたくことになります。
その時にprocess.cwd()が返す値はpath/to/projectになるので、次のように1つ変数化しておけばルート相対URLのような感覚でパス名を記述することができます。

<% const ROOT = `${process.cwd()}/src`; %>
<%- include(`${ROOT}/path/to/_include.ejs`) -%>
<%- include(`${ROOT}/path/to/_include.ejs`) -%>
<%- include(`${ROOT}/path/to/_include.ejs`) -%>
<%- include(`${ROOT}/path/to/_include.ejs`) -%>
4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0