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-njsVerified import paths — ran on the pinned version, not inferred.
Shows installation, Babel config, and a simple njs script using for-of and object spread, both of which are transformed by the preset.
Upgrade njs to at least 0.7.0 or use an older version of the preset (0.2.1 max).
Avoid using dynamic imports and generators in njs code, or provide your own custom plugin.
Review the plugin list in the preset's README to see which features are now natively supported.
Use import babelPresetNjs from 'babel-preset-njs' or use require in a CommonJS context.
Avoid optional chaining; use if statements or add @babel/plugin-proposal-optional-chaining manually.
Update to babel-preset-njs@0.7.0 and ensure @babel/core is version 7+.