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 traverseVerified import paths — ran on the pinned version, not inferred.
Demonstrates collecting leaf nodes using `reduce` and performing an in-place transformation on numbers using `forEach`.
Evaluate modern alternatives like Lodash (`_.get`, `_.set`, `_.forIn`), immer for immutable updates, or custom recursive functions for specific traversal needs.
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.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.
For CommonJS, use `const traverse = require('traverse');`. For ESM (if compatible), use `import traverse from 'traverse';` or `import * as traverse from 'traverse';`.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`).
No dependency data recorded yet.