\href{https://travis-ci.org/inxilpro/node-app-root-path}{\texttt{ }} \href{https://david-dm.org/inxilpro/node-app-root-path}{\texttt{ }} \href{https://codecov.io/gh/inxilpro/node-app-root-path}{\texttt{ }}

\begin{quote}
{\bfseries{Please Note\+:}} Due to the very limited scope of this module, I do not anticipate needing to make very many changes to it. Expect long stretches of zero updates—that does not mean that the module is outdated. \end{quote}
This simple module helps you access your application\textquotesingle{}s root path from anywhere in the application without resorting to relative paths like {\ttfamily require(\char`\"{}../../path\char`\"{})}.\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3073}{}\doxysection{Installation}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3073}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm i -\/S app-\/root-\/path }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3074}{}\doxysection{Usage}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3074}
To simply access the app\textquotesingle{}s root path, use the module as though it were a string\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var appRoot = require('app-\/root-\/path');}
\DoxyCodeLine{var myModule = require(appRoot + '/lib/my-\/module.js'); }

\end{DoxyCode}


\begin{quote}
{\itshape Side note\+: the module actually returns an object, but that object implements the {\ttfamily to\+String} method, so you can use it as though it were a string. There are a few edge cases where this might not be the case (most notably {\ttfamily console.\+log}), but they shouldn\textquotesingle{}t affect actual use of the module, where you\textquotesingle{}re almost always concatenating with an additional string.} \end{quote}
A helper function is also provided\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var reqlib = require('app-\/root-\/path').require;}
\DoxyCodeLine{var myModule = reqlib('/lib/my-\/module.js'); }

\end{DoxyCode}


It\textquotesingle{}s a little hacky, but you can also put this method on your application\textquotesingle{}s {\ttfamily global} object to use it everywhere in your project\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{// In app.js}
\DoxyCodeLine{global.reqlib = require('app-\/root-\/path').require;}
\DoxyCodeLine{}
\DoxyCodeLine{// In lib/module/component/subcomponent.js}
\DoxyCodeLine{var myModule = reqlib('/lib/my-\/module.js'); }

\end{DoxyCode}


Finally, you can also just resolve a module path\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var myModulePath = require('app-\/root-\/path').resolve('/lib/my-\/module.js'); }

\end{DoxyCode}


You can explicitly set the path, using the environmental variable {\ttfamily APP\+\_\+\+ROOT\+\_\+\+PATH} or by calling `require(\textquotesingle{}app-\/root-\/path').set\+Path(\textquotesingle{}/my/app/is/here\textquotesingle{})\`{}\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3075}{}\doxysection{How It Works (under the hood)}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3075}
\begin{quote}
No need to read this unless you\textquotesingle{}re curious—or you run into a (very unlikely) case where the module does not work as expected. \end{quote}
This module uses two different methods to determine the app\textquotesingle{}s root path, depending on the circumstances.\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3076}{}\doxysubsection{Primary Method}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3076}
If the module is located inside your project\textquotesingle{}s directory, somewhere within the {\ttfamily node\+\_\+modules} directory (whether directly, or inside a submodule), we effectively do (the actual code takes cross-\/platform path names/etc into consideration)\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{path.resolve(\_\_dirname).split('/node\_modules')[0]; }

\end{DoxyCode}


This will take a path like {\ttfamily /var/www/node\+\_\+modules/submodule/node\+\_\+modules/app-\/root-\/path} and return {\ttfamily /var/www}. In nearly all cases, this is just what you need.\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3077}{}\doxysubsection{Secondary Method (for edge cases)}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3077}
The node module loader will also look in a few other places for modules (for example, ones that you install globally with {\ttfamily npm install -\/g}). These can be in one of\+:


\begin{DoxyItemize}
\item {\ttfamily \$\+HOME/.node\+\_\+modules}
\item {\ttfamily \$\+HOME/.node\+\_\+libraries}
\item {\ttfamily \$\+PREFIX/lib/node}
\end{DoxyItemize}

Or, anywhere in the {\ttfamily NODE\+\_\+\+PATH} environmental variable (\href{http://nodejs.org/api/modules.html\#modules_loading_from_the_global_folders}{\texttt{ see documentation}}).

In these cases, we fall back to an alternate trick\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{path.dirname(require.main.filename); }

\end{DoxyCode}


When a file is run directly from Node, {\ttfamily require.\+main} is set to that file\textquotesingle{}s {\ttfamily module}. Each module has a {\ttfamily filename} property that refers to the filename of that module, so by fetching the directory name for that file, we at least get the directory of file passed to {\ttfamily node}. In some cases (process managers and test suites, for example) this doesn\textquotesingle{}t actually give the correct directory, though, so this method is only used as a fallback.\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3078}{}\doxysubsection{Edge-\/\+Case\+: Global CLIs}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3078}
If your module is installed as a global CLI, for example in {\ttfamily /usr/local/lib/node\+\_\+modules/yourmodule}, then {\ttfamily require.\+main.\+filename} will report {\ttfamily /usr/local/lib/node\+\_\+modules/yourmodule/bin}, which is probably not what you want. {\ttfamily app-\/root-\/path} is aware of this edge-\/case and will strip the {\ttfamily /bin} automatically.\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3079}{}\doxysection{Change Log}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3079}
\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3080}{}\doxysubsection{3.\+0.\+0}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3080}

\begin{DoxyItemize}
\item Improved Yarn Plug\textquotesingle{}n\textquotesingle{}Play support
\item Fixed bug when used with webpack
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3081}{}\doxysubsection{2.\+2.\+1}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3081}

\begin{DoxyItemize}
\item Better handling of webpack
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3082}{}\doxysubsection{2.\+2.\+0}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3082}

\begin{DoxyItemize}
\item Added support for Yarn Plug\textquotesingle{}n\textquotesingle{}Play
\item Adjusted browser-\/shim to address webpack warnings
\item Bumped minimum Node version to 6
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3083}{}\doxysubsection{2.\+0.\+1}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3083}

\begin{DoxyItemize}
\item Minor tweaks to how electron-\/specific logic runs. Should help with packagers that try to resolve all {\ttfamily require()} statements during packaging.
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3084}{}\doxysubsection{2.\+0.\+0}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3084}

\begin{DoxyItemize}
\item Removed official support for node $<$ 4.\+0
\item Removed support for passing {\ttfamily module.\+require} to {\ttfamily app\+Root\+Path.\+require} (which has been deprecated for a while)
\item Implemented \href{https://github.com/semantic-release/semantic-release}{\texttt{ semantic-\/release}} from here on out
\item Added browserify-\/compatible shim
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3085}{}\doxysubsection{1.\+3.\+0}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3085}

\begin{DoxyItemize}
\item Updated \href{https://github.com/atom/electron}{\texttt{ electron}} to match changes in version 1.\+0 of that project
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3086}{}\doxysubsection{1.\+2.\+1}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3086}

\begin{DoxyItemize}
\item Had to bump package version because 1.\+2.\+0 got published to npm as @beta
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3087}{}\doxysubsection{1.\+2.\+0}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3087}

\begin{DoxyItemize}
\item Special logic to resolve correctly when in an \href{https://github.com/atom/electron}{\texttt{ electron}} renderer process
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3088}{}\doxysubsection{1.\+1.\+0}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3088}

\begin{DoxyItemize}
\item Special logic to handle an edge case when used in a globally-\/installed CLI project
\item Fixed a bug where {\ttfamily set\+Path()} did not update `require(\textquotesingle{}app-\/root-\/path').path{\ttfamily }
\item {\ttfamily Moved some logic outside of the}resolve()\`{} function so that it\textquotesingle{}s not called multiple times
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3089}{}\doxysubsection{1.\+0.\+0}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3089}

\begin{DoxyItemize}
\item No changes. Just updated the version to signify a locked API (see \href{http://semver.org/}{\texttt{ semver}}).
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3090}{}\doxysubsection{0.\+1.\+1}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3090}

\begin{DoxyItemize}
\item Added Windows support (and, theoretically, other operating systems that have a directory separator that\textquotesingle{}s not \char`\"{}/\char`\"{})
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3091}{}\doxysubsection{0.\+1.\+0}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3091}

\begin{DoxyItemize}
\item Completely rewrote the path resolution method to account for most possible scenarios. This shouldn\textquotesingle{}t cause and backwards compatibility issues, but always test your code.
\item Removed the need to pass a modules\textquotesingle{}s {\ttfamily require()} method to the {\ttfamily app\+Root\+Path.\+require()} function. Which it\textquotesingle{}s true that each module has its own {\ttfamily require()} method, in practice it doesn\textquotesingle{}t matter, and it\textquotesingle{}s {\bfseries{much}} simpler this way.
\item Added tests
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_app_root_path_README_autotoc_md3092}{}\doxysection{Development Nodes}\label{md_sfcode_node_modules_app_root_path_README_autotoc_md3092}
When using \href{https://github.com/semantic-release/semantic-release}{\texttt{ semantic-\/release}}, the preferred method for commits is\+:


\begin{DoxyItemize}
\item {\ttfamily git add …}
\item {\ttfamily git cz} (see \href{https://github.com/commitizen/cz-cli}{\texttt{ commitizen}})
\item {\ttfamily git push}
\end{DoxyItemize}

This helps ensure that commits match the expected format. Commits to {\ttfamily master} will cause releases. 