I am unable to deploy a nodejs/ejs application on netlify

Hi!
For few weeks, I have been trying to deploy a nodejs as backend and ejs (html) as front-end app on netlify. There is no video or blog available for how I should proceed. Please help me out. Thanks in advance!

1 Like

Hi @Arshitabhatt, and welcome.

Can you give us details on what you have tried or maybe a link to a GitHub repository if public? It will be hard to help you out, when we have no idea what you are really trying to accomplish.

Note: Netlify does not have a website backend structure like nodejs. Nodejs is used for the build environment during the CI/CD workflow to build the site being deployed to Netlify.

1 Like

@talves
Thanks a lot for the reply !
This github repo for my project GitHub - Arshitabhatt/readsnet-nodejs: This is a simple static webapp
As of now the front-end is on ejs template but later I will shift to a MERN stack app. Though there are some hints given for how to deploy a Mern app but I had a hard time following them. I think, a demo project would be helpful

1 Like

Netlify does not have a website backend structure like nodejs. Nodejs is used for the build environment during the CI/CD workflow to build the site being deployed to Netlify.

A MERN stack would not be supported on Netlify. You would not be able to host the app.js solution on Netlify, but I do have a partial solution for you to create a static site from your example repository.

Partial Solution

Create a script to build your page using node and ejs at the time of the Netlify build.

/build.js

const fs = require("fs");
const path = require("path");
const ejs = require("ejs");

function ejs2html({ path, outPath, data, options }) {
  fs.readFile(path, "utf8", function(err, data) {
    if (err) {
      console.log(err);
      return false;
    }
    ejs.renderFile(path, data, options, (err, html) => {
      if (err) {
        console.log(err);
        return false;
      }
      fs.writeFile(outPath, html, function(err) {
        if (err) {
          console.log(err);
          return false;
        }
        return true;
      });
    });
  });
}

ejs2html({
  path: `${__dirname}/views/index.ejs`,
  outPath: `${__dirname}/public/index.html`
});

This script will build an index.html static page for your site into the public folder and you can then use it to deploy a static version of the ejs view for index.ejs.

The /netlify.toml could be used as:

[build]
  command = "node ./build.js"
  publish = "public"

Caveats

This is not a MERN stack site deployed to Netlify. It is a static generated site using ejs.

1 Like

@Arshitabhatt the solution here is not a recommendation, but an example of creating the ejs page to a static site for deploy on Netlify. A more advanced setup would need to be used to create a fully data connected app using MongoDB for data on your site hosted on Netlify. Also, this solution does not solve the issue of multiple pages, but could be done with some modifications.

1 Like

Tony’s advice is good and better than what I’m about to say here for your purposes, almost certainly!

Another way to approach it is shown in this blog post describes a way to run your JS code dynamically using our functions feature, but that is NOT a straightforward port from a traditional node app!

1 Like

@talves @fool
Thank you for the replies and support. I will workout on this solution and will let you know.

Hello @Arshitabhatt , I know it’s been a long time but I have the same issue. Did you ever manage to deploy your EJS app to netlify?

Hey there, @reloaded22 :wave:

Let us know if you find the information you are looking for! Happy building.