Registry / devops / express-rest-i18n

express-rest-i18n

JSON →
library1.0.1jsnpmunverified

Dead simple i18n middleware for Express REST APIs. Version 1.0.1 provides lightweight, dependency-free internationalization with nested message support and automatic locale detection via query, header, or cookie. No build tools, no extra dependencies; simply define messages in JSON and use the `t()` method on `req` or `res`. Unlike heavier i18n libraries, it focuses solely on REST APIs with a minimal footprint. Released as stable, with no active development but no known issues.

npm install express-rest-i18n
INSTALL
IMPORT
SIG · EXPRESS-REST-I18N
E
express-rest-i18n
devopsjavascriptv1.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

default
import i18nCreate from 'express-rest-i18n';
const i18nCreate = require('express-rest-i18n');
ESM import using default export. CommonJS require also works as the package exports a single function.
i18nCreate
const i18nCreate = require('express-rest-i18n');
import { i18nCreate } from 'express-rest-i18n';
CommonJS style - the package exports a single function as default; named import is incorrect.
t() method on res/req
const text = res.i18n.t('key');
const text = req.t('key');
The `t()` method is available on `res.i18n` or `req.i18n`, not directly on `req` or `res`.

Creates an i18n middleware, registers it, and uses the t() method in a route.

import express from 'express'; import i18nCreate from 'express-rest-i18n'; const app = express(); const i18n = i18nCreate({ defaultLocale: 'en', allowFallback: true, messages: { en: { greeting: 'Hello' }, 'pt-br': { greeting: 'Olá' }, }, }); app.use(i18n.middleware); app.get('/', (req, res) => { const msg = res.i18n.t('greeting'); res.send(msg); }); app.listen(3000);
Debug
Known issues
gotchaMiddleware should be added after body-parser to recognize parsed body for locale detection.
fix
Ensure app.use(i18n.middleware) comes after app.use(bodyParser.json()) or similar.
affects: >=1.0.0
breakingThe t() method is attached to res.i18n and req.i18n, not directly on res or req.
fix
Use res.i18n.t('key') or req.i18n.t('key') instead of res.t('key') or req.t('key').
affects: >=1.0.0
gotchaWhen a key is not found, t() returns the key string itself, not an error or undefined.
fix
Check if the returned value equals the key to detect missing translations.
affects: >=1.0.0
gotchaLocale detection order: query param (locale), header (Accept-Language), then cookie (locale). Unknown behavior if multiple sources conflict.
fix
Ensure only one locale source is used or understand the priority: query > header > cookie > defaultLocale.
affects: >=1.0.0
gotchaallowFallback: true (default) means missing keys fall back to defaultLocale, not chain fallback through multiple locales.
fix
Set allowFallback: false if you want strict locale enforcement.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot read properties of undefined (reading 't')
Middleware not added or added before body-parser, so res.i18n or req.i18n is undefined.
fix
Ensure app.use(i18n.middleware) is called after body-parser and before routes.
t is not a function
Attempting to call t() directly on req or res instead of req.i18n or res.i18n.
fix
Use req.i18n.t('key') or res.i18n.t('key').
import i18nCreate from 'express-rest-i18n'; SyntaxError: Cannot use import statement outside a module
Using ESM import in a CommonJS project without proper configuration.
fix
Use const i18nCreate = require('express-rest-i18n'); or add "type": "module" to package.json.
module.exports = function(...) - TypeError: i18nCreate is not a function
Incorrect import: using named import for a default export.
fix
Use default import (import i18nCreate from ...) or const i18nCreate = require(...).
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
expressrequiredPeer dependency - middleware requires Express app
Agent activity
2 hits · last 30 days
node
2
Resources
express-rest-i18n — npm install express-rest-i18n · libregistry