Registry / serialization / babel-template

babel-template

JSON →
library6.26.0jsnpmunverified

Generate an AST from a string template (quasiquote implementation). Current stable version is 6.26.0 (legacy Babel 6). Release cadence: infrequent; part of Babel monorepo, which has moved on to v7/v8. Key differentiator: allows building AST nodes using template syntax instead of manual node construction, but only works with Babel 6 AST types. For Babel 7+, use @babel/template instead.

npm install babel-template
INSTALL
IMPORT
SIG · BABEL-TEMPLATE
B
babel-template
serializationjavascriptv6.26.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

default
import template from 'babel-template'
const template = require('babel-template').default
Package has a CommonJS default export. ESM import works directly as default.
default
const template = require('babel-template')
const { default: template } = require('babel-template')
In CJS, require returns the function directly, not an object with default property.
template
import template from 'babel-template'
import { template } from 'babel-template'
There is no named export 'template'. Use default import.

Shows how to create a template function using babel-template and invoke it with AST node replacements.

import template from 'babel-template'; import generate from 'babel-generator'; import * as t from 'babel-types'; const buildRequire = template(` var IMPORT_NAME = require(SOURCE); `); const ast = buildRequire({ IMPORT_NAME: t.identifier('myModule'), SOURCE: t.stringLiteral('my-module') }); console.log(generate(ast).code); // var myModule = require('my-module');
Debug
Known issues
deprecatedbabel-template is only compatible with Babel 6. For Babel 7+, use @babel/template.
fix
Migrate to @babel/template: npm install @babel/template --save-dev and update imports to import template from '@babel/template'.
affects: >=6.0.0 <7.0.0
gotchaTemplate placeholders (e.g., IMPORT_NAME) must be valid JavaScript identifiers and must appear in the template exactly as they are used (case-sensitive).
fix
Ensure placeholders are all uppercase or follow identifier rules; avoid using keywords.
affects: >=6.0.0
gotchababel-template relies on babylon (Babel 6 parser) which does not support all modern JavaScript syntax (e.g., optional chaining, nullish coalescing).
fix
Use @babel/template with @babel/parser for modern syntax support.
affects: >=6.0.0
Errors
Common errors & fixes
Cannot find module 'babel-template'
Package not installed or wrong package name (e.g., Babel 7+ users might try to use this instead of @babel/template).
fix
Run 'npm install --save-dev babel-template' for Babel 6, or use '@babel/template' for Babel 7+.
TypeError: template is not a function
Incorrect import attempt, e.g., import { template } from 'babel-template'.
fix
Use default import: import template from 'babel-template'.
Upgrade
Version history
6.26.0latest on npm
Audit
Dependencies
babel-typesrequiredrequired for AST node type references in template replacements
babylonrequiredused internally to parse the template string into AST
Agent activity
18 hits · last 30 days
node
16
OpenAI (training)
1
Resources
babel-template — npm install babel-template · libregistry