{"id":13592,"library":"neotraverse","title":"Object Traversal and Transformation","description":"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.","status":"active","version":"0.6.18","language":"javascript","source_language":"en","source_url":"https://github.com/PuruVJ/neotraverse","tags":["javascript","traverse","walk","recursive","map","forEach","deep","clone","typescript"],"install":[{"cmd":"npm install neotraverse","lang":"bash","label":"npm"},{"cmd":"yarn add neotraverse","lang":"bash","label":"yarn"},{"cmd":"pnpm add neotraverse","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This is the default build, ESM-only (ES2022). It provides the same API as the original `traverse` package.","wrong":"const traverse = require('neotraverse');","symbol":"traverse","correct":"import traverse from 'neotraverse';"},{"note":"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`.","wrong":"import traverse from 'neotraverse/modern';","symbol":"Traverse","correct":"import { Traverse } from 'neotraverse/modern';"},{"note":"For compatibility with older Node.js versions or browser environments (ES5) and CommonJS projects. An ESM import is also available for this build.","wrong":"import traverse from 'neotraverse/legacy';","symbol":"traverse","correct":"const traverse = require('neotraverse/legacy');"}],"quickstart":{"code":"import { Traverse } from 'neotraverse/modern';\n\nconst obj = [5, 6, -3, [7, 8, -2, 1], { f: 10, g: -13 }];\n\n// Create a new Traverse instance for the object\nconst traverser = new Traverse(obj);\n\n// Iterate over all nodes and update negative numbers in-place\ntraverser.forEach(function (ctx, x) {\n  if (typeof x === 'number' && x < 0) {\n    ctx.update(x + 128);\n  }\n});\n\nconsole.log(obj);\n// Expected output: [ 5, 6, 125, [ 7, 8, 126, 1 ], { f: 10, g: 115 } ]","lang":"typescript","description":"Demonstrates how to use the modern `Traverse` class to recursively update negative numbers within an object/array structure in-place."},"warnings":[{"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.","message":"The 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')`.","severity":"breaking","affected_versions":">=0.1.0"},{"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); });`","message":"When 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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Remove `@types/traverse` from your project's dependencies.","message":"The `@types/traverse` package is no longer necessary when using `neotraverse` because the library ships with native TypeScript definitions.","severity":"deprecated","affected_versions":">=0.1.0"}],"env_vars":null,"search_vec":"'0.6.18':56 '1.38':77 'activ':62 'aim':43 'api':45,115,125 'approach':98 'architectur':71 'argument':132 'array':23 'broader':142 'brot':80 'build':84,103,109,121,136 'clone':151 'commonj':140 'compat':46,110,143 'context':127 'current':52 'deep':12,150 'default':105 'depend':70 'design':10 'differenti':65 'distinct':102 'elimin':88 'es5':138 'esm':96,107,119 'esm-first':95 'esm-on':106,118 'explicit':131 'first':97 'footprint':73 'foreach':149 'fork':35 'improv':124 'in-plac':15 'includ':66 'indic':61 'introduc':48 'javascript':20,144 'javascript/typescript':8 'js':40 'js-travers':39 'kb':78 'key':64 'legaci':135 'librari':9 'low':75 'mainten':63 'map':148 'min':79 'minim':72 'modern':50,83,117 'nativ':85 'need':90 'neotravers':5 'object':1,21 'offer':122 'origin':113 'packag':42 'pass':128 'patch':59 'place':17 'popular':38 'provid':100 'recent':58 'recurs':26,147 'releas':60 'rewrit':33 'signific':49 'stabl':53 'stand':29 'support':87,137 'three':101 'transform':4,18 'travers':2,13,41,114,145 'types/traverse':92 'typescript':32,86,152 'version':54 'walk':27,146 'zero':69 'zero-depend':68","created_at":"2026-04-20T01:55:17.849802+00:00","updated_at":"2026-04-20T01:55:17.849802+00:00","problems":[{"fix":"Change your callback function signature to accept `ctx` as the first argument, e.g., `(ctx, x) => { ctx.update(newValue); }`.","cause":"Attempting to use `this.update()` within a callback when importing from `neotraverse/modern`.","error":"TypeError: this.update is not a function"},{"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.","cause":"Trying to `import traverse from 'neotraverse'` in a CommonJS (CJS) project setup without `\"type\": \"module\"` in `package.json`.","error":"SyntaxError: Cannot use import statement outside a module"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/PuruVJ/neotraverse","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/neotraverse","openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization","data"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}