Registry / serialization / traverse

traverse

JSON →
library1.0.0jsnpmunverified

The `traverse` package provides utility functions for deep traversal and transformation of JavaScript objects. It allows developers to recursively walk through object graphs, including arrays and nested objects, and perform operations such as in-place modification, deep cloning, and collection of specific nodes. Key features include explicit handling of circular references, various traversal methods like `map`, `forEach`, `reduce`, `paths`, and `nodes`, and a rich `this` context within callbacks providing information about the current node, its parent, path, and depth. The current stable version is 0.6.11, last updated several years ago, indicating an inactive release cadence. Its primary differentiator is its robust context object during traversal and explicit circular reference management. It is primarily a CommonJS module, which may require specific handling in modern ESM-only environments.

npm install traverse
INSTALL
IMPORT
SIG · TRAVERSE
T
traverse
serializationjavascriptv1.0.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.

traverse
const traverse = require('traverse');
import traverse from 'traverse';
This package is primarily CommonJS. While `import traverse from 'traverse'` might work with some bundlers or Node.js versions, `require` is the intended and most reliable import mechanism for direct Node.js usage. Named imports (e.g., `import { traverse } ...`) are incorrect.
traverse (ESM fallback)
import traverse from 'traverse';
import { traverse } from 'traverse';
For ESM environments, this pattern might work via Node.js's CJS interoperability or bundlers, effectively treating the CJS default export as an ESM default. However, direct named imports are not supported as the package does not explicitly export named members.
traverse (TypeScript)
import traverse = require('traverse');
import * as traverse from 'traverse';
When using TypeScript without a `@types/traverse` package, the CommonJS-style `import = require()` syntax is the most robust way to import this module. If `@types/traverse` were available, `import traverse from 'traverse';` would likely be correct.

Demonstrates collecting leaf nodes using `reduce` and performing an in-place transformation on numbers using `forEach`.

const traverse = require('traverse'); const obj = { a : [1,2,3], b : 4, c : [5,6], d : { e : [7,8], f : 9 }, }; // Collect all leaf nodes from the object const leaves = traverse(obj).reduce(function (acc, x) { if (this.isLeaf) { acc.push(x); } return acc; }, []); console.log('Original object:', JSON.stringify(obj)); console.log('Collected leaf nodes:', leaves); // Example of in-place transformation: double all numbers traverse(obj).forEach(function (x) { if (typeof x === 'number') { this.update(x * 2); } }); console.log('Object after in-place transformation:', JSON.stringify(obj));
Debug
Known issues
deprecatedThe `traverse` package (version 0.6.11) has not been updated for several years and appears to be unmaintained. It is not recommended for new projects. Consider actively maintained alternatives for object traversal and manipulation in modern JavaScript environments.
fix
Evaluate modern alternatives like Lodash (`_.get`, `_.set`, `_.forIn`), immer for immutable updates, or custom recursive functions for specific traversal needs.
affects: >=0.6.0
gotchaThe package primarily uses CommonJS (`require`). Direct ESM `import` statements may require bundler configuration or Node.js's CJS interop, which can be inconsistent or lead to unexpected behavior without proper setup.
fix
For CommonJS projects, use `const traverse = require('traverse');`. For ESM projects, if you must use it, prefer `import traverse from 'traverse';` and ensure your build tool or Node.js environment correctly handles CJS module interoperability. Be aware that named imports will not work.
affects: >=0.4
gotchaBe mindful of the difference between `.map()` and `.forEach()`. `.map()` returns a *new* object with updated values, while `.forEach()` modifies the *original* object in-place when `this.update()` is called. Unintended side effects can occur if this distinction is not understood.
fix
Always use `.map()` when you need a new object and want to preserve the original. Use `.forEach()` specifically when you intend to mutate the original object in-place.
affects: >=0.4
Errors
Common errors & fixes
TypeError: traverse is not a function
Attempting to use a named import (e.g., `import { traverse } from 'traverse'`) when the package only provides a default export (in a CJS context, this means the `module.exports` value).
fix
For CommonJS, use `const traverse = require('traverse');`. For ESM (if compatible), use `import traverse from 'traverse';` or `import * as traverse from 'traverse';`.
Error: Cannot find module 'traverse'
This error typically indicates that the package is not installed (`npm install traverse` was not run) or that a bundler/resolver is misconfigured to find CommonJS modules, especially in an ESM-only project setup.
fix
Run `npm install traverse` or `yarn add traverse`. If using TypeScript, ensure your `tsconfig.json` allows importing CommonJS modules (e.g., `"esModuleInterop": true`, `"allowSyntheticDefaultImports": true`).
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
traverse — npm install traverse · libregistry