green1111
green1111

Reputation: 231

How to Include EJS Template Files in a Node.js Executable

I'm developing a Node.js web application using Express and EJS as the view engine. My goal is to create a single executable file that I can run on my webserver without needing to copy any additional folders or files.

Here's my setup:

My Express setup looks like this:

path = require('path');
const express = require('express');
const app = express();

app.set('views', path.join(__dirname, 'view'));
app.set('view engine', 'ejs');

// Routes
app.get('/', (req, res) => {
  res.render("myfile");
});

When I copy only the .exe file to my webserver and try to run it, I get the following error:

Error: Failed to lookup view "myfile" in views directory "C:\snapshot\WebApp\build\view"

How can I correctly include the myfile.ejs file in my executable using pkg?

Upvotes: 1

Views: 154

Answers (1)

traynor
traynor

Reputation: 8667

View templates fall under assets (non-JavaScript files) that are not included automatically, and need to be specified in pkg property of package.json.

So, in your package.json, add assets property that has your views directory as a value (in your case name of the views directory is view):

 "pkg": {
    "assets": "view/**/*"
  }  

source:

Such cases are not handled by pkg. So you must specify the files - scripts and assets - manually in pkg property of your package.json file.

  "pkg": {
    "scripts": "build/**/*.js",
    "assets": "views/**/*",
    "targets": [ "node14-linux-arm64" ],
    "outputPath": "dist"
  }

https://www.npmjs.com/package/pkg#config

Upvotes: 1

Related Questions