i18next-scanner-webpack

raw JSON →
1.0.0 verified Sat Apr 25 auth: no javascript

A webpack plugin that automatically extracts i18n translation keys from your source code using i18next-parser. Integrates with webpack 5.x and requires Node.js >= 14.16.0. Version 1.0.0 includes breaking changes from v0.9.0: replaced the underlying scanner with i18next-parser. The plugin scans specified file extensions for translation function calls like t() and generates locale JSON files. Supports both ESM and CommonJS configs, async mode for faster dev builds, and customizable lexers for different file types. Differentiators: simple setup, tight webpack integration, and active maintenance.

error Cannot find module 'i18next-parser'
cause Missing peer dependency i18next-parser when using v0.9.0+.
fix
Run npm install i18next-parser --save-dev or check that it is in your dependencies.
error TypeError: I18nextWebpackPlugin is not a constructor
cause Using `.default` on CommonJS require: `const Plugin = require('...').default` returns undefined.
fix
Use const I18nextWebpackPlugin = require('i18next-scanner-webpack'); without .default.
error Error: Configuration error. Cannot find 'options.locales'
cause Options not nested under `options` key in plugin config, e.g. `{ locales: ['en'] }` instead of `{ options: { locales: ['en'] } }`.
fix
Wrap i18next-parser options in an options property: new I18nextWebpackPlugin({ options: { locales: ['en'] } }).
error Error: Unsupported file extension '.ts'
cause TypeScript files not scanned by default; need lexer configuration.
fix
Add lexers for TypeScript: options.lexers.ts: [{ lexer: 'JavascriptLexer', functions: ['t'] }].
breaking v0.9.0 switched underlying scanner from i18next-scanner to i18next-parser. Config structure changed — options must now match i18next-parser format.
fix Migrate config to use i18next-parser options (see v0.9.0 changelog). Example: replace `func` with `lexers`, `lngs` with `locales`, etc.
breaking v1.0.0 requires Webpack 5.x. Earlier versions (0.x) may support Webpack 4.
fix Upgrade to Webpack 5 if using v1.0.0+. For Webpack 4, use v0.8.3 or earlier.
gotcha If `async` option is set to `true`, webpack may finish before translation files are written — be cautious in production builds.
fix Only use `async: true` in development mode. For production, set `async: false` (default).
gotcha CommonJS require without .default: `const Plugin = require('i18next-scanner-webpack')` works. Using `.default` will result in `undefined`.
fix Remove `.default` when requiring via CommonJS.
deprecated Versions <=0.8.3 use deprecated i18next-scanner package. Configs from those versions are incompatible with v0.9.0+.
fix Update to v0.9.0+ and migrate config to i18next-parser format.
npm install i18next-scanner-webpack
yarn add i18next-scanner-webpack
pnpm add i18next-scanner-webpack

Sets up i18next-scanner-webpack in webpack 5 with ESM config, scanning .js and .jsx files for translation keys, outputting English and German locale files.

import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
import I18nextWebpackPlugin from 'i18next-scanner-webpack';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

export default {
  mode: 'development',
  entry: path.resolve(__dirname, './src/index.js'),
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js'
  },
  plugins: [
    new I18nextWebpackPlugin({
      src: './src',
      dest: './locales',
      extensions: ['.js', '.jsx'],
      options: {
        lexers: {
          js: [{
            lexer: 'JavascriptLexer',
            functions: ['t', '$t', 'i18next.t', 'i18n.t']
          }]
        },
        locales: ['en', 'de'],
        output: '$LOCALE/$NAMESPACE.json'
      }
    })
  ]
};