utc – Underscore Template Compiler (CLI)
raw JSON → 0.1.0 verified Fri May 01 auth: no javascript deprecated
utc is a CLI tool that compiles Underscore.js-style templates into AMD-compatible JavaScript functions. Version 0.1.0 (released 2012) is the only release. It precompiles templates to avoid runtime parsing, suitable for build-time optimization. No active maintenance or updates exist. The tool converts template strings containing <%= ... %> into compiled functions that use with() and concatenated output.
Common errors
error require('utc') is not a function ↓
cause utc is a CLI tool only; no module exports.
fix
Use the global command utc, e.g., from terminal: utc -d ./templates -s Template -f Str
error ENOENT: no such file or directory, open '...'? ↓
cause The -d directory path does not exist or contains no files matching the suffix.
fix
Ensure directory exists and file suffix matches: e.g., -s Template looks for *Template.js files.
error TypeError: Cannot read property '1' of null ↓
cause Template file does not follow the expected AMD pattern shown in the readme.
fix
Template must be a function that returns an object with methods containing Underscore template strings.
Warnings
deprecated Package has not been updated since 2012; no support for modern ECMAScript features or module systems beyond AMD. ↓
fix Use modern alternatives like Lodash _.template, handlebars precompilation, or EJS.
gotcha CLI modifies files in-place! Use version control or backups before running. ↓
fix Copy templates to a build directory first, or use a different build tool that outputs to a separate folder.
gotcha Only works with AMD-style modules (define). No CommonJS, ES module, or global export support. ↓
fix Write templates in AMD format or use a wrapper script to convert output.
breaking Uses with(obj) statement in compiled code, which is forbidden in strict mode and may cause performance issues or errors in modern JS environments. ↓
fix Use a template engine that avoids with(), like Lodash (with option) or Handlebars.
gotcha CLI arguments must exactly match: -d (directory), -s (suffix), -f (function suffix). No configuration file support. ↓
fix Wrap in a shell script or npm script to avoid repeated typing.
Install
npm install utc yarn add utc pnpm add utc Imports
- utc CLI wrong
Trying to require('utc') in Node.js codecorrectRun globally: utc -d "templates" -s "Template" -f "Str"
Quickstart
# Install globally
npm install -g utc@0.1.0
# Create a template file
echo '(function () {
define(function () {
return {
getListTemplateStr: function () {
return this._helper() + ": <%= name %>";
},
_helper: function () {
return "hello";
}
};
});
}).call(this);' > templates/sampleTemplate.js
# Run the compiler
utc -d "templates" -s "Template" -f "Str"