{"id":19316,"library":"babel-preset-njs","title":"Babel Preset njs","description":"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.","status":"active","version":"0.7.0","language":"javascript","source_language":"en","source_url":"https://github.com/jirutka/babel-preset-njs","tags":["javascript","babel-preset","njs","nginx"],"install":[{"cmd":"npm install babel-preset-njs","lang":"bash","label":"npm"},{"cmd":"yarn add babel-preset-njs","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-preset-njs","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency required for Babel integration","package":"@babel/core","optional":false}],"imports":[{"note":"ESM import; can also be used with require in CJS","wrong":"const babelPresetNjs = require('babel-preset-njs')","symbol":"babelPresetNjs","correct":"import babelPresetNjs from 'babel-preset-njs'"},{"note":"In Babel config, reference the preset by its name string, not by directly requiring it.","wrong":"module.exports = { presets: [require('babel-preset-njs')] }","symbol":"babel-preset-njs (preset name string)","correct":"module.exports = { presets: ['babel-preset-njs'] }"},{"note":"Do not prefix with @babel/. The preset name is exactly 'babel-preset-njs'.","wrong":"{\n  \"presets\": [\"@babel/preset-njs\"]\n}","symbol":"babel.config.js (using preset via string)","correct":"{\n  \"presets\": [\"babel-preset-njs\"]\n}"}],"quickstart":{"code":"// Install: npm install --save-dev @babel/core babel-preset-njs\n// Then create babel.config.js:\nmodule.exports = {\n  presets: ['babel-preset-njs']\n};\n\n// Now write your njs script (src/index.js):\nconst arr = [1, 2, 3];\nconst obj = { a: 1, b: 2 };\nconst spread = { ...obj, c: 3 };\nfor (const x of arr) {\n  ngx.log(ngx.INFO, x);\n}\n\n// Run: npx babel src/index.js --out-dir dist\n// Result will be compatible with njs (e.g., for-of transpiled, object spread transformed).","lang":"javascript","description":"Shows installation, Babel config, and a simple njs script using for-of and object spread, both of which are transformed by the preset."},"warnings":[{"fix":"Upgrade njs to at least 0.7.0 or use an older version of the preset (0.2.1 max).","message":"Version 0.7.0 requires njs ≥ 0.7.0. Older versions of njs may not work.","severity":"breaking","affected_versions":">=0.7.0 <0.8.0"},{"fix":"Avoid using dynamic imports and generators in njs code, or provide your own custom plugin.","message":"The preset does NOT transform dynamic import() or generators (yield) as njs does not support them. These will fail at runtime.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Review the plugin list in the preset's README to see which features are now natively supported.","message":"Some plugins included may become unnecessary as njs evolves. Check compatibility table before upgrading.","severity":"deprecated","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use import babelPresetNjs from 'babel-preset-njs' or use require in a CommonJS context.","cause":"Using require() to load the preset in an ES module environment.","error":"ReferenceError: require is not defined in ES module scope"},{"fix":"Avoid optional chaining; use if statements or add @babel/plugin-proposal-optional-chaining manually.","cause":"Optional chaining (?.) is not yet supported by njs and not included in the preset's plugins.","error":"SyntaxError: Unexpected token '.' (when using optional chaining)"},{"fix":"Update to babel-preset-njs@0.7.0 and ensure @babel/core is version 7+.","cause":"Using an older version of the preset that exports an object instead of a function.","error":"Error: Plugin/Preset files are not allowed to export objects, only functions"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}