Registry / devops / babel-gettext-extractor

babel-gettext-extractor

JSON →
library4.1.3jsnpmunverified

Babel plugin to extract gettext strings from JavaScript/JSX code, supporting ES6+ and JSX syntax. Current stable version is 4.1.3, maintained by Sentry. It is a fork of babel-gettext-plugin with added support for references and earlier Node.js versions. Works with Babel 7. Key differentiators: flexible configuration of function names, custom output file naming via a callback, base directory for relative paths, and optional whitespace stripping from template literals. Alternatives like babel-plugin-react-intl focus on React Intl, while this plugin targets the standard gettext workflow.

npm install babel-gettext-extractor
INSTALL
IMPORT
SIG · BABEL-GETTEXT-EXTR
B
babel-gettext-extractor
devopsjavascriptv4.1.3
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.

plugin
module.exports = { plugins: ['babel-gettext-extractor'] }
import babelGettextExtractor from 'babel-gettext-extractor'
This is a Babel plugin, not a standalone module. It's used via Babel's plugins array, not imported directly in application code.
plugin with options
module.exports = { plugins: [['babel-gettext-extractor', { headers: { 'content-type': 'text/plain; charset=UTF-8' } }]] }
module.exports = { plugins: ['babel-gettext-extractor', { headers: {} }] }
Options are passed as an array entry [plugin, options], not as separate arguments.
require in code
const babel = require('babel'); babel.transform(code, { plugins: ['babel-gettext-extractor'] })
const babelGettextExtractor = require('babel-gettext-extractor'); babel.transform(code, { plugins: [babelGettextExtractor] })
The plugin is a string reference; requiring it directly and using the object is incorrect because Babel resolves it by name from node_modules.

Configures babel-gettext-extractor with custom function names and headers, outputs a .pot file.

// .babelrc or babel.config.js module.exports = { plugins: [ ['babel-gettext-extractor', { headers: { 'content-type': 'text/plain; charset=UTF-8', 'plural-forms': 'nplurals=2; plural=(n!=1);' }, functionNames: { _t: ['msgid'], _n: ['msgid', 'msgid_plural'], _p: ['msgctxt', 'msgid'] }, fileName: './messages.pot', baseDirectory: './src', stripTemplateLiteralIndent: false }] ] }; // Run: babel --plugins babel-gettext-extractor src/ -d lib/
Debug
Known issues
breakingPlugin name changed from 'babel-gettext-plugin' to 'babel-gettext-extractor' in v4.
fix
Replace 'babel-gettext-plugin' with 'babel-gettext-extractor' in your Babel config.
affects: >=4.0.0
deprecatedThe 'babel' package is deprecated; use '@babel/core' with Babel 7.
fix
Replace require('babel') with require('@babel/core') and update config accordingly.
affects: >=4.0.0
gotchafileName option can be a function returning false to skip extraction per file.
fix
Return false from the fileName function to prevent output for that file.
affects: >=4.0.0
gotchaIf stripTemplateLiteralIndent is true, gettext implementations must also strip leading indent; otherwise translations may have extra whitespace.
fix
Ensure your gettext runtime also strips leading indent when using this option.
affects: >=4.0.0
gotchabaseDirectory only affects file references in the .pot output; it does not filter which files are processed.
fix
Use fileName function or Babel's ignore/include patterns to control which files are processed.
affects: >=4.0.0
Errors
Common errors & fixes
Error: Cannot find module 'babel-gettext-extractor'
Plugin not installed or misspelled in Babel config.
fix
Run 'npm install --save-dev babel-gettext-extractor' and verify the plugin string is correct.
ReferenceError: [BABEL] unknown: Unknown option: .plugins[0][1].foo
Invalid option name in the plugin configuration object.
fix
Check the option names: valid ones are 'headers', 'functionNames', 'fileName', 'baseDirectory', 'stripTemplateLiteralIndent'.
TypeError: Cannot read property 'sourceFileName' of undefined
fileName function receives an object without the expected property, possibly due to Babel version mismatch.
fix
Ensure you are using Babel 7 and pass the file object correctly; check the example in the README.
No output .pot file generated
Either no translatable strings found, fileName returns false, or output path is incorrect (e.g., relative path resolves to a non-existent directory).
fix
Ensure your source files contain calls to configured functionNames with string literals. Check console logs for skipped files. Confirm fileName path is absolute or relative to the working directory.
Upgrade
Version history
4.1.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
6
Resources
babel-gettext-extractor — npm install babel-gettext-extractor · libregistry