title: How to use a hbs template on Firebase Storage with nodemailer-express-handlebars in a database onCreate trigger function?
body: <p>I am trying to use a handlebars template file uploaded to my firebase project storage (appId.appspot.com/templates/testTemplate.hbs) with nodemailer to send an email when an onCreate function is triggered on a realtime database node.</p>
  
  <p>I can send emails successfully using html formatted string but really need to use a template to add dynamic data into the email.</p>
  
  <p>Here my function:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import * as functions from "firebase-functions";
  const admin = require("firebase-admin");
  const hbs = require("nodemailer-express-handlebars");
  const nodemailer = require("nodemailer");
  
  const smtpConfig = {
    host: "mailHost",
    port: 465,
    secure: true,
    auth: {
      user: "xxxxxxxx",
      pass: "xxxxxxxx"
    }
  };
  
  const transporter = nodemailer.createTransport(smtpConfig);
  
  exports.sendEmail = functions.database
    .ref("/databasePath/{pushId}")
    .onCreate(async (snapshot, context) =&gt; {
      const userData = snapshot.val();
      admin.initializeApp({
        storageBucket: "appId.appspot.com"
      });
      const bucket = admin.storage().bucket();
      const templatesFolder = bucket.name + "/templates/"; // path to storage folder with templates
  
      transporter.use(
        "compile",
        hbs({
          viewPath: templatesFolder,
          extName: ".hbs"
        })
      );
      const uniqueCode = "generated by a function";
      const uniqueLink = "https://appId.firebaseapp.com/?id=" + uniqueCode;
      const message = {
        from: "fromEmail",
        to: "toEmail",
        subject: "Subject",
        template: "testTemplate", // name of the template file
        context: {
          user: "User name",
          link: uniqueLink
        }
      };
  
      try {
        await transporter.sendMail(message);
        console.log("Email sent to:", "toEmail");
      } catch (error) {
        console.error("Error sending email:", error);
      }
      return null;
    });</code></pre>
  </div>
  </div>
  </p>
  
  <p>When the function is triggered I get the following error in the logs:</p>
  
  <p>There was an error while sending the email: { Error: ENOENT: no such file or directory, open '/user_code/appId.appspot.com/templates/testTemplate.hbs'
      at Error (native)
    errno: -2,
    code: 'ENOENT',
    syscall: 'open',
    path: '/user_code/appId.appspot.com/templates/testTemplate.hbs' }</p>
  
  <p>The bucket.name has '/user_code' at the start so hbs can't find the template.  How can I get the right path to the templates folder?</p>