Home > Net >  Error: Failed to lookup view "home" in views directory
Error: Failed to lookup view "home" in views directory

Time:04-03

I developed a small application with electron and expressjs. In development mode, the app works fine. But after packaging the app for the production mode I got the error that the home view is not found. Here are some details: The main route in app.js

router.get('/', async (req,res) => {
    res.render('home');
});

My file structure is: enter image description here

In order to solve the problem I tried the following:

 router.get('/', async (req,res) => {
    var path = require('path');
    res.render(path.join(__dirname '/views/home.ejs'));
});

Now the view is loaded, but all the css and js files within home.ejs are not loaded

enter image description here

All css and js files are located under the public folder and within home.ejs, i refer to these files as follows:

<script src="/js/vendor/jquery-3.6.0.min.js"></script>
<script src="/js/vendor/bootstrap.bundle.min.js"></script>
<script src="/js/home.js"></script>

I tried both electron packagers, electron-forge and electron-builder and had the same error. Can you please help??

CodePudding user response:

You need an express.static(...) statement somewhere in your app to serve the css and js files. How does that look? The following should work:

app.use("/js", express.static(__dirname   "/public/js"));
app.use("/css", express.static(__dirname   "/public/css"));
  • Related