{"id":11266,"library":"map2tree","title":"map2tree","description":"map2tree is a utility library that provides a pure function to convert a flat map structure into a hierarchical tree. Its primary use case is within the `redux-devtools-chart-monitor` package, influencing its specific opinions on how data is transformed. It does not convert objects and arrays deeply nested within collections into tree structures and offers support for Immutable.js List and Map data types. The current stable version, 4.0.0, was released as an ESM-only package. The library is part of the `reduxjs/redux-devtools` monorepo, suggesting its release cadence is tied to the broader Redux DevTools ecosystem.","status":"active","version":"4.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/reduxjs/redux-devtools","tags":["javascript","map2tree","map-to-tree","mapToTree","map","tree","typescript"],"install":[{"cmd":"npm install map2tree","lang":"bash","label":"npm"},{"cmd":"yarn add map2tree","lang":"bash","label":"yarn"},{"cmd":"pnpm add map2tree","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The `map2tree` function is the default export. Version 4.0.0 and later are ESM-only, so CommonJS `require()` is not supported.","wrong":"const map2tree = require('map2tree');","symbol":"map2tree","correct":"import map2tree from 'map2tree';"}],"quickstart":{"code":"import map2tree from 'map2tree';\n\nconst someMap = {\n  someReducer: {\n    todos: [\n      { title: 'map', someNestedObject: { foo: 'bar' } },\n      { title: 'to', someNestedArray: ['foo', 'bar'] },\n      { title: 'tree' },\n      { title: 'map2tree' },\n    ],\n    completedCount: 1,\n  },\n  otherReducer: {\n    foo: 0,\n    bar: { key: 'value' },\n  },\n};\n\nconst treeOutput = map2tree(someMap, {\n  key: 'state', // the name you want for the root node of the output tree\n  pushMethod: 'push', // use 'unshift' to change the order children nodes are added\n});\n\nconsole.log(JSON.stringify(treeOutput, null, 2));\n/*\nOutput example:\n{\n  \"name\": \"state\",\n  \"children\": [\n    {\n      \"name\": \"someReducer\",\n      \"children\": [\n        {\n          \"name\": \"todos\",\n          \"children\": [\n            {\n              \"name\": \"todo[0]\",\n              \"object\": {\n                \"title\": \"map\",\n                \"someNestedObject\": {\n                  \"foo\": \"bar\"\n                }\n              }\n            },\n            {\n              \"name\": \"todo[1]\",\n              \"object\": {\n                \"title\": \"to\",\n                \"someNestedArray\": [\n                  \"foo\",\n                  \"bar\"\n                ]\n              }\n            },\n            {\n              \"name\": \"todo[2]\",\n              \"object\": {\n                \"title\": \"tree\"\n              }\n            },\n            {\n              \"name\": \"todo[3]\",\n              \"object\": {\n                \"title\": \"map2tree\"\n              }\n            }\n          ]\n        },\n        {\n          \"name\": \"completedCount\",\n          \"value\": 1\n        }\n      ]\n    },\n    {\n      \"name\": \"otherReducer\",\n      \"children\": [\n        {\n          \"name\": \"foo\",\n          \"value\": 0\n        },\n        {\n          \"name\": \"bar\",\n          \"object\": {\n            \"key\": \"value\"\n          }\n        }\n      ]\n    }\n  ]\n}\n*/","lang":"typescript","description":"Demonstrates how to import and use `map2tree` with a sample map, specifying root key and child order, and logs the resulting tree structure."},"warnings":[{"fix":"Migrate your project to use ES modules (e.g., set `\"type\": \"module\"` in `package.json`) and use `import` statements, or transpile your code with a bundler like Webpack or Rollup that supports ESM.","message":"`map2tree` is now an ESM-only package. Direct `require()` statements for CommonJS environments will fail.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Be aware of this specific behavior. If full deep recursion for all nested structures is required, `map2tree` may not be suitable without pre-processing or a different utility.","message":"Objects and arrays deeply nested within collections are intentionally not converted into a tree structure. They remain as plain objects/arrays within the 'object' property of the node.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure you are only using Immutable.js `List` and `Map` if you expect them to be transformed. For other Immutable.js types, convert them to plain JavaScript objects or arrays before passing them to `map2tree` if you need them included in the tree.","message":"The library provides limited support for Immutable.js data structures, specifically only `List` and `Map`. Other Immutable.js types will not be processed correctly into the tree structure.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Update your import statement to `import map2tree from 'map2tree';` and ensure your project or bundler is configured for ES modules.","cause":"Attempting to `require()` `map2tree` in a CommonJS environment when version 4.0.0 and above are ESM-only.","error":"ERR_REQUIRE_ESM"},{"fix":"Use the correct default import: `import map2tree from 'map2tree';` or verify that your CommonJS `require()` is being transpiled correctly if using an older version that supported it.","cause":"Incorrect import of `map2tree`, potentially due to trying to use named import for a default export or incorrect CommonJS usage in an ESM context.","error":"TypeError: map2tree is not a function"}],"ecosystem":"npm"}