tmpl JavaScript Micro Templating
The `tmpl` package, version 1.0.5, is a minimalist JavaScript micro-templating utility last updated in 2013. Its primary function is basic string interpolation, replacing `{}` placeholders in a template string with corresponding values from a provided data object. This library offers a very limited feature set compared to more robust templating engines like Handlebars or EJS, lacking control flow, iteration, or helper functions. Developed before the widespread availability of native JavaScript template literals (ES6 backticks), `tmpl` is now largely superseded by built-in language features for new development. The project is considered abandoned, with no active maintenance, security updates, or new releases since its last version over a decade ago. While still accumulating significant weekly downloads, this is primarily due to its presence as a transitive dependency in older projects. It functions exclusively within CommonJS environments and offers no native support for ECMAScript Modules (ESM) or TypeScript.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` in a native ES module environment (e.g., when `package.json` has `"type": "module"`).fixEnsure your project is configured for CommonJS or use a bundler that can handle CommonJS modules within an ESM context. For new code, prefer native ES module `import` statements and template literals. -
TypeError: tmpl is not a function
cause Incorrectly attempting to invoke `tmpl` as a constructor (`new tmpl(...)`) or destructuring it incorrectly (e.g., `const { tmpl } = require('tmpl');`).fix`tmpl` exports a single function as its default. Ensure you are importing it correctly as a default CommonJS export and calling it directly: `const result = tmpl('...', data);`.
Warnings
- breaking The `tmpl` package is abandoned, with its last release (1.0.5) over a decade ago in 2013. This poses significant security risks due to unpatched vulnerabilities and ensures incompatibility with modern JavaScript features and environments.
- gotcha This package is designed for CommonJS environments and does not provide native ECMAScript Module (ESM) support. Attempting to `import tmpl from 'tmpl'` in a Node.js project configured for native ESM (`'type': 'module'` in `package.json`) will result in a runtime error.
- gotcha No TypeScript type definitions (`.d.ts` files) are shipped with the `tmpl` package, nor are they available on DefinitelyTyped. Using `tmpl` in a TypeScript project will result in type errors.
- deprecated Native JavaScript template literals (introduced in ES6/ES2015) provide a more idiomatic, performant, and secure way to achieve string interpolation (e.g., `` `Hello ${name}!` `` vs. `tmpl('Hello {name}!', { name })`).
Install
-
npm install tmpl -
yarn add tmpl -
pnpm add tmpl
Imports
- tmpl
import tmpl from 'tmpl';
const tmpl = require('tmpl'); - tmpl (destructuring)
const { tmpl } = require('tmpl');const renderTemplate = require('tmpl');
Quickstart
const tmpl = require('tmpl');
const templateString = 'Hello, {name}! Your age is {age}.';
const data = { name: 'Alice', age: 30 };
const result = tmpl(templateString, data);
console.log(result);
// Expected output: Hello, Alice! Your age is 30.
// Example with missing data - placeholders are left as is
const incompleteResult = tmpl('Missing: {missingKey}', { name: 'Bob' });
console.log(incompleteResult);
// Expected output: Missing: {missingKey}