npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

express-hbs-compile

v1.0.2

Published

Compile Handlebars templates into HTML strings, powered by express-hbs.

Downloads

33

Readme

express-hbs-compile

Compile Handlebars templates into HTML strings, powered by express-hbs.

Installation

npm install express-hbs-compile

Quick Start

Import the package and call compile() with the path to your template directory. This returns an async render function that compiles a Handlebars template into an HTML string.

import path from 'path';
import compile from 'express-hbs-compile';

// 1. Create a render function by specifying where your templates live.
const render = compile({
  viewsDir: path.join(import.meta.dirname, 'views'),
});

// 2. Render a template with data — returns a Promise<string>.
const html = await render('index.hbs', {title: 'Home'});
console.log(html); // => compiled HTML string

Note: import.meta.dirname requires Node 20.11+. For older versions, use __dirname (CommonJS) or dirname(fileURLToPath(import.meta.url)) (ESM).

CommonJS is also supported: const compile = require('express-hbs-compile');

API

compile(options)

Creates a render function bound to the given options. All paths except viewsDir have sensible defaults and are optional.

const render = compile({
  viewsDir: path.join(import.meta.dirname, 'views'),
  partialsDir: path.join(import.meta.dirname, 'views/partials'),
  layoutsDir: path.join(import.meta.dirname, 'views/layout'),
  defaultLayout: path.join(import.meta.dirname, 'views/layout/default.hbs'),
  extname: '.hbs',
  helpers: {
    shout: str => str.toUpperCase() + '!',
  },
});

Options

| Name | Type | Required | Default | Description | |------|------|----------|---------|-------------| | viewsDir | string | Yes | | Absolute path to the template directory. | | partialsDir | string \| string[] | No | <viewsDir>/partials | Absolute path(s) to partial template directories. | | layoutsDir | string | No | <viewsDir>/layout | Absolute path to the layout template directory. | | defaultLayout | string | No | <layoutsDir>/default.hbs | Absolute path to the default layout file. | | extname | string | No | .hbs | File extension for templates. | | contentHelperName | string | No | contentFor | Name of the content block helper. | | blockHelperName | string | No | block | Name of the block rendering helper. | | helpers | {[name: string]: Function} | No | | Custom Handlebars helpers to register. |

Returns

(template: string, data?: object) => Promise<string> — an async render function.

Throws

  • TypeError — if viewsDir is not provided.
  • TypeError — if any specified directory or file does not exist.

render(template, data?)

The function returned by compile(). Compiles a template and returns the resulting HTML string.

// Relative path (resolved from viewsDir)
const html = await render('user/profile.hbs', {username: 'John'});

// Absolute path also works
const welcome = await render(path.join(import.meta.dirname, 'views/email/welcome.hbs'), {
  recipientName: 'John',
});

Parameters

| Name | Type | Required | Description | |------|------|----------|-------------| | template | string | Yes | File name or absolute path to the template. Relative names are resolved from viewsDir. | | data | object | No | Data passed to the template. settings, cache, and layout are reserved keys. |

Returns

Promise<string> — the compiled HTML string.

Throws

  • TypeError — if the template file does not exist.
  • TypeError — if data contains a reserved key (settings, cache, or layout).

Built-in Helpers

The internal Handlebars instance is extended with handlebars-extd, which provides additional helpers like eq, ne, gt, gte, lt, lte, and more.

{{#if (eq role 'admin')}}
  <span>Admin</span>
{{/if}}

Changelog

See CHANGELOG.md.

Author

shumatsumonobu

License

MIT