Registry / serialization / neotraverse

neotraverse

JSON →
library0.6.18jsnpmunverified

neotraverse is a JavaScript/TypeScript library designed for deep traversal and in-place transformation of JavaScript objects and arrays through a recursive walk. It stands as a TypeScript rewrite and fork of the popular `js-traverse` package, aiming for API compatibility while introducing significant modernizations. The current stable version is 0.6.18, with recent patch releases indicating active maintenance. Key differentiators include its zero-dependency architecture, minimal footprint (as low as 1.38KB min+brotli for the modern build), native TypeScript support (eliminating the need for `@types/traverse`), and an ESM-first approach. It provides three distinct builds: a default ESM-only build compatible with the original `traverse` API, a 'modern' ESM-only build offering an improved API with context passed as an explicit argument, and a 'legacy' build supporting ES5 and CommonJS for broader compatibility.

npm install neotraverse
INSTALL
IMPORT
SIG · NEOTRAVERSE
N
neotraverse
serializationjavascriptv0.6.18
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.

traverse
import traverse from 'neotraverse';
const traverse = require('neotraverse');
This is the default build, ESM-only (ES2022). It provides the same API as the original `traverse` package.
Traverse
import { Traverse } from 'neotraverse/modern';
import traverse from 'neotraverse/modern';
The modern build is ESM-only (ES2022) and provides a class-based API where traversal context is passed as the first argument `ctx` instead of `this`.
traverse
const traverse = require('neotraverse/legacy');
import traverse from 'neotraverse/legacy';
For compatibility with older Node.js versions or browser environments (ES5) and CommonJS projects. An ESM import is also available for this build.

Demonstrates how to use the modern `Traverse` class to recursively update negative numbers within an object/array structure in-place.

import { Traverse } from 'neotraverse/modern'; const obj = [5, 6, -3, [7, 8, -2, 1], { f: 10, g: -13 }]; // Create a new Traverse instance for the object const traverser = new Traverse(obj); // Iterate over all nodes and update negative numbers in-place traverser.forEach(function (ctx, x) { if (typeof x === 'number' && x < 0) { ctx.update(x + 128); } }); console.log(obj); // Expected output: [ 5, 6, 125, [ 7, 8, 126, 1 ], { f: 10, g: 115 } ]
Debug
Known issues
breakingThe default `neotraverse` package is ESM-only (ES2022). Projects still using CommonJS (CJS) or older Node.js versions (pre-18 for default/modern builds) will experience import errors if attempting to `require('neotraverse')`.
fix
For CJS or ES5 environments, use `require('neotraverse/legacy')`. For older Node.js versions (>=10, <18), use `neotraverse/legacy` with ESM or CJS imports.
affects: >=0.1.0
gotchaWhen using the `neotraverse/modern` build, the traversal context is passed as the first argument `ctx` to the callback function (e.g., `forEach((ctx, x) => ...)`), replacing the `this` context used in the original `traverse` and `neotraverse` default/legacy builds.
fix
Always use the `ctx` argument for methods like `ctx.update()`, `ctx.isLeaf()`, etc., in the modern build. Example: `new Traverse(obj).forEach((ctx, x) => { if (x < 0) ctx.update(x + 128); });`
affects: >=0.1.0
deprecatedThe `@types/traverse` package is no longer necessary when using `neotraverse` because the library ships with native TypeScript definitions.
fix
Remove `@types/traverse` from your project's dependencies.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: this.update is not a function
Attempting to use `this.update()` within a callback when importing from `neotraverse/modern`.
fix
Change your callback function signature to accept `ctx` as the first argument, e.g., `(ctx, x) => { ctx.update(newValue); }`.
SyntaxError: Cannot use import statement outside a module
Trying to `import traverse from 'neotraverse'` in a CommonJS (CJS) project setup without `"type": "module"` in `package.json`.
fix
If your project is CJS, use `const traverse = require('neotraverse/legacy');`. If you intend to use ESM, ensure your `package.json` specifies `"type": "module"` or use `.mjs` file extensions.
Upgrade
Version history
0.6.18latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
neotraverse — npm install neotraverse · libregistry