\href{https://travis-ci.org/npm/read-package-tree}{\texttt{ }}

Read the contents of node\+\_\+modules.\hypertarget{md_sfcode_node_modules_read_package_tree_README_autotoc_md23964}{}\doxysection{USAGE}\label{md_sfcode_node_modules_read_package_tree_README_autotoc_md23964}

\begin{DoxyCode}{0}
\DoxyCodeLine{var rpt = require ('read-\/package-\/tree')}
\DoxyCodeLine{rpt('/path/to/pkg/root', function (node, kidName) \{}
\DoxyCodeLine{  // optional filter function– if included, each package folder found is passed to}
\DoxyCodeLine{  // it to see if it should be included in the final tree}
\DoxyCodeLine{  // node is what we're adding children to}
\DoxyCodeLine{  // kidName is the directory name of the module we're considering adding}
\DoxyCodeLine{  // return true -\/> include, false -\/> skip}
\DoxyCodeLine{\}, function (er, data) \{}
\DoxyCodeLine{  // er means that something didn't work.}
\DoxyCodeLine{  // data is a structure like:}
\DoxyCodeLine{  // \{}
\DoxyCodeLine{  //   package: <package.json data, or an empty object>}
\DoxyCodeLine{  //   package.name: defaults to `basename(path)`}
\DoxyCodeLine{  //   children: [ <more things like this> ]}
\DoxyCodeLine{  //   parent: <thing that has this in its children property, or null>}
\DoxyCodeLine{  //   path: <path loaded>}
\DoxyCodeLine{  //   realpath: <the real path on disk>}
\DoxyCodeLine{  //   isLink: <set if this is a Link>}
\DoxyCodeLine{  //   target: <if a Link, then this is the actual Node>}
\DoxyCodeLine{  //   error: <if set, the error we got loading/parsing the package.json>}
\DoxyCodeLine{  // \}}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{// or promise-\/style}
\DoxyCodeLine{rpt('/path/to/pkg/root').then(data => \{ ... \}) }

\end{DoxyCode}


That\textquotesingle{}s it. It doesn\textquotesingle{}t figure out if dependencies are met, it doesn\textquotesingle{}t mutate package.\+json data objects (beyond what \href{http://npm.im/read-package-json}{\texttt{ read-\/package-\/json}} already does), it doesn\textquotesingle{}t limit its search to include/exclude {\ttfamily dev\+Dependencies}, or anything else.

Just follows the links in the {\ttfamily node\+\_\+modules} hierarchy and reads the package.\+json files it finds therein.\hypertarget{md_sfcode_node_modules_read_package_tree_README_autotoc_md23965}{}\doxysection{Symbolic Links}\label{md_sfcode_node_modules_read_package_tree_README_autotoc_md23965}
When there are symlinks to packages in the {\ttfamily node\+\_\+modules} hierarchy, a {\ttfamily Link} object will be created, with a {\ttfamily target} that is a {\ttfamily Node} object.

For the most part, you can treat {\ttfamily Link} objects just the same as {\ttfamily Node} objects. But if your tree-\/walking program needs to treat symlinks differently from normal folders, then make sure to check the object.

In a given {\ttfamily read-\/package-\/tree} run, a specific {\ttfamily path} will always correspond to a single object, and a specific {\ttfamily realpath} will always correspond to a single {\ttfamily Node} object. This means that you may not be able to pass the resulting data object to {\ttfamily JSON.\+stringify}, because it may contain cycles.\hypertarget{md_sfcode_node_modules_read_package_tree_README_autotoc_md23966}{}\doxysection{Errors}\label{md_sfcode_node_modules_read_package_tree_README_autotoc_md23966}
Errors parsing or finding a package.\+json in node\+\_\+modules will result in a node with the error property set. We will still find deeper node\+\_\+modules if any exist. {\itshape Prior to {\ttfamily 5.\+0.\+0} these aborted tree reading with an error callback.}

Only a few classes of errors are fatal (result in an error callback)\+:


\begin{DoxyItemize}
\item If the top level location is entirely missing, that will error.
\item if {\ttfamily fs.\+realpath} returns an error for any path its trying to resolve. 
\end{DoxyItemize}