npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

future-root-path

v1.0.0

Published

Determine an app's root path from anywhere inside the app

Downloads

5

Readme

App Root Path Module

Build Status Dependency Status Coverage Status

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.

This simple module helps you access your application's root path from anywhere in the application without resorting to relative paths like require("../../path").

Installation

$ npm install app-root-path --save

Usage

To simply access the app's root path, use the module as though it were a string:

var appRoot = require('app-root-path');
var myModule = require(appRoot + '/lib/my-module.js');

Side note: the module actually returns an object, but that object implements the toString 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 console.log), but they shouldn't affect actual use of the module, where you're almost always concatenating with an additional string.

A helper function is also provided:

var reqlib = require('app-root-path').require;
var myModule = reqlib('/lib/my-module.js');

It's a little hacky, but you can also put this method on your application's global object to use it everywhere in your project:

// In app.js
global.reqlib = require('app-root-path').require;

// In lib/module/component/subcomponent.js
var myModule = reqlib('/lib/my-module.js');

Finally, you can also just resolve a module path:

var myModulePath = require('app-root-path').resolve('/lib/my-module.js');

You can explicitly set the path, using the environmental variable APP_ROOT_PATH or by calling require('app-root-path').setPath('/my/app/is/here')

How It Works (under the hood)

No need to read this unless your curious—or you run into a (very unlikely) case where the module does not work as expected.

This module uses two different methods to determine the app's root path, depending on the circumstances.

Primary Method

If the module is located inside your project's directory, somewhere within the node_modules directory (whether directly, or inside a submodule), we effectively do (the actual code takes cross-platform path names/etc into consideration):

path.resolve(__dirname).split('/node_modules')[0];

This will take a path like /var/www/node_modules/submodule/node_modules/app-root-path and return /var/www. In nearly all cases, this is just what you need.

Secondary Method (for edge cases)

The node module loader will also look in a few other places for modules (for example, ones that you install globally with npm install -g). These can be in one of:

  • $HOME/.node_modules
  • $HOME/.node_libraries
  • $PREFIX/lib/node

Or, anywhere in the NODE_PATH environmental variable (see documentation).

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

path.dirname(require.main.filename);

When a file is run directly from Node, require.main is set to that file's module. Each module has a 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 node. In some cases (process managers and test suites, for example) this doesn't actually give the correct directory, though, so this method is only used as a fallback.

Change Log

1.0.0

  • No changes. Just updated the version to signify a locked API (see semver).

0.1.1

  • Added Windows support (and, theoretically, other operating systems that have a directory separator that's not "/")

0.1.0

  • Completely rewrote the path resolution method to account for most possible scenarios. This shouldn't cause and backwards compatibility issues, but always test your code.
  • Removed the need to pass a modules's require() method to the appRootPath.require() function. Which it's true that each module has its own require() method, in practice it doesn't matter, and it's much simpler this way.
  • Added tests