Registry / devops / babel-preset-njs

babel-preset-njs

JSON →
library0.7.0jsnpmunverified

A Babel preset for transforming modern JavaScript (up to ES2021) into code compatible with njs (NGINX JavaScript). Current stable version is 0.7.0, targeting njs ≥ 0.7.0, with peer dependency on @babel/core ^7.0.0-0. Key differentiator: provides a curated set of Babel plugins for features not yet supported by njs (e.g., classes, for-of, object rest/spread), while skipping transformations for features njs already supports (e.g., arrow functions, block scoping, destructuring). Ideal for developers writing njs scripts with modern JS syntax.

npm install babel-preset-njs
INSTALL
IMPORT
SIG · BABEL-PRESET-NJS
B
babel-preset-njs
devopsjavascriptv0.7.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.

babelPresetNjs
import babelPresetNjs from 'babel-preset-njs'
const babelPresetNjs = require('babel-preset-njs')
ESM import; can also be used with require in CJS
babel-preset-njs (preset name string)
module.exports = { presets: ['babel-preset-njs'] }
module.exports = { presets: [require('babel-preset-njs')] }
In Babel config, reference the preset by its name string, not by directly requiring it.
babel.config.js (using preset via string)
{ "presets": ["babel-preset-njs"] }
{ "presets": ["@babel/preset-njs"] }
Do not prefix with @babel/. The preset name is exactly 'babel-preset-njs'.

Shows installation, Babel config, and a simple njs script using for-of and object spread, both of which are transformed by the preset.

// Install: npm install --save-dev @babel/core babel-preset-njs // Then create babel.config.js: module.exports = { presets: ['babel-preset-njs'] }; // Now write your njs script (src/index.js): const arr = [1, 2, 3]; const obj = { a: 1, b: 2 }; const spread = { ...obj, c: 3 }; for (const x of arr) { ngx.log(ngx.INFO, x); } // Run: npx babel src/index.js --out-dir dist // Result will be compatible with njs (e.g., for-of transpiled, object spread transformed).
Debug
Known issues
breakingVersion 0.7.0 requires njs ≥ 0.7.0. Older versions of njs may not work.
fix
Upgrade njs to at least 0.7.0 or use an older version of the preset (0.2.1 max).
affects: >=0.7.0 <0.8.0
gotchaThe preset does NOT transform dynamic import() or generators (yield) as njs does not support them. These will fail at runtime.
fix
Avoid using dynamic imports and generators in njs code, or provide your own custom plugin.
affects: >=0.1.0
deprecatedSome plugins included may become unnecessary as njs evolves. Check compatibility table before upgrading.
fix
Review the plugin list in the preset's README to see which features are now natively supported.
affects: >=0.1.0
Errors
Common errors & fixes
ReferenceError: require is not defined in ES module scope
Using require() to load the preset in an ES module environment.
fix
Use import babelPresetNjs from 'babel-preset-njs' or use require in a CommonJS context.
SyntaxError: Unexpected token '.' (when using optional chaining)
Optional chaining (?.) is not yet supported by njs and not included in the preset's plugins.
fix
Avoid optional chaining; use if statements or add @babel/plugin-proposal-optional-chaining manually.
Error: Plugin/Preset files are not allowed to export objects, only functions
Using an older version of the preset that exports an object instead of a function.
fix
Update to babel-preset-njs@0.7.0 and ensure @babel/core is version 7+.
Upgrade
Version history
0.7.0latest on npm
Audit
Dependencies
@babel/corerequiredPeer dependency required for Babel integration
Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
babel-preset-njs — npm install babel-preset-njs · libregistry