Registry / web-framework / ejs-loader

ejs-loader

JSON →
library0.5.0jsnpmunverified

Webpack loader that compiles EJS templates using lodash/underscore's _.template function. Current stable version is 0.5.0, with maintenance releases every few years. Key differentiator from ejs-compiled-loader: it uses lodash/underscore template syntax, not the EJS templating engine. Requires ProvidePlugin for global '_'. Supports custom delimiters via options, and ES module export by default with an option to switch to CommonJS. The 'variable' option is required to avoid strict mode errors with ES modules.

npm install ejs-loader
INSTALL
IMPORT
SIG · EJS-LOADER
E
ejs-loader
web-frameworkjavascriptv0.5.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
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
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (loader)
✓ const template = require('ejs-loader!./file.ejs');
✗ import template from 'ejs-loader!./file.ejs';
Webpack loaders are not ESM-compatible in import statements; use require() for loader-returned modules.
default (template function)
✓ const compiled = require('ejs!./file.ejs'); const html = compiled({ name: 'John' });
✗ const html = require('ejs!./file.ejs')({ name: 'John' });
The returned module is a function that you call with data; it is not the rendered HTML.
webpack config (module.rules)
✓ module.exports = { module: { rules: [ { test: /\.ejs$/, use: ['ejs-loader'] } ] } };
✗ module.exports = { module: { loaders: [ { test: /\.ejs$/, loader: 'ejs-loader' } ] } };
Webpack 4+ uses 'module.rules' and 'use' array; the old 'module.loaders' with 'loader' string is deprecated.

Shows a minimal webpack 4+ config with ejs-loader, ProvidePlugin for lodash, and usage of the compiled template function.

// webpack.config.js const webpack = require('webpack'); module.exports = { entry: './src/index.js', module: { rules: [ { test: /\.ejs$/, use: [ { loader: 'ejs-loader', options: { variable: 'data' } } ] } ] }, plugins: [ new webpack.ProvidePlugin({ _: 'lodash' }) ] }; // src/index.js const template = require('./template.ejs'); const html = template({ name: 'World' }); document.body.innerHTML = html; // src/template.ejs <h1>Hello <%= data.name %>!</h1>
Debug
Known issues
gotchaThe 'variable' option is required for the loader to work with ES module exports; otherwise strict mode errors occur.
fix
Add 'variable: 'data'' to loader options in webpack config.
affects: >=0.5.0
deprecatedUsing 'module.loaders' array and 'loader' string is deprecated in webpack 4+.
fix
Use 'module.rules' array with 'use' array and 'loader' string inside object.
affects: >=0.3.1
gotchaThe loader returns a template function, not the rendered HTML string; you must call it with data.
fix
Call the required module as a function: const html = template(data);
affects: >=0.3.0
gotchaRequires a global '_' variable (lodash/underscore) at runtime; use ProvidePlugin.
fix
Add new webpack.ProvidePlugin({ _: 'lodash' }) to plugins in webpack config.
affects: >=0.3.0
Errors
Common errors & fixes
Module not found: Error: Cannot resolve module 'ejs-loader'
ejs-loader is not installed.
fix
Run 'npm install ejs-loader --save-dev'.
Error: ejs-loader: You must provide a 'variable' option when using ES modules.
Missing 'variable' option in loader configuration with esModule: true (default).
fix
Add options: { variable: 'data' } to the loader rule.
Uncaught ReferenceError: _ is not defined
Lodash/underscore is not provided globally at runtime.
fix
Add new webpack.ProvidePlugin({ _: 'lodash' }) to webpack plugins.
Uncaught TypeError: template is not a function
The required module is not being used as a function; it's the compiled template function.
fix
Call the required module: const html = require('./template.ejs')({ data });
Upgrade
Version history
0.5.0latest on npm
Audit
Dependencies
lodashrequiredRuntime dependency for template compilation
Agent activity
6 hits · last 30 days
node
6
Resources
ejs-loader — npm install ejs-loader · libregistry