Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
twigjs-loader (default)
✓ import indexView from './index.twig'
✗ const indexView = require('./index.twig')
ESM import works with webpack module rules; CommonJS require also works but not recommended for consistency.
ExpressView
✓ import { ExpressView } from 'twigjs-loader'
✗ const ExpressView = require('twigjs-loader').ExpressView
ESM named export; CommonJS require is also valid but less idiomatic.
renderTemplate option
✓ use: { loader: 'twigjs-loader', options: { renderTemplate(twigData, dependencies, isHot) { ... } } }
✗ use: { loader: 'twigjs-loader', options: { renderTemplate: (twigData, dependencies, isHot) => { ... } } }
Both arrow and regular functions work; ensure the function returns a string of JavaScript code.
Basic setup: webpack config with twigjs-loader, importing a Twig template in ESM, and rendering it with data.
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.twig$/,
use: {
loader: 'twigjs-loader',
options: {
renderTemplate(twigData, dependencies, isHot) {
return `
${dependencies}
var twig = require("twig").twig;
var tpl = twig(${JSON.stringify(twigData)});
module.exports = function(context) { return tpl.render(context); };
`;
},
},
},
},
],
},
};
// index.js
import indexView from './index.twig';
document.body.innerHTML = indexView({ name: 'World' });
// index.twig
<h1>Hello {{ name }}!</h1>
Errors
Common errors & fixes
Module not found: Error: Can't resolve 'twig' in '...'
Missing peer dependency 'twig'.
Error: Template not found: ... .twig
Webpack cannot resolve the template path relative to the importing file.
fixUse absolute paths or configure webpack resolve.modules/resolve.alias.
TypeError: Cannot read property 'render' of undefined
The twig object is not initialized correctly in custom renderTemplate.
fixEnsure 'require("twig").twig' returns a function and that twigData is valid. Audit
Dependencies
twigrequiredRuntime dependency for template rendering; must be installed separately (peer dependency).