app-root-path (2024)

app-root-path (1) app-root-path (2) app-root-path (3)

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 i -S app-root-path

Usage

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

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.jsglobal.reqlib = require('app-root-path').require;// In lib/module/component/subcomponent.jsvar 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 you're 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 circ*mstances.

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.

Edge-Case: Global CLIs

If your module is installed as a global CLI, for example in /usr/local/lib/node_modules/yourmodule, thenrequire.main.filename will report /usr/local/lib/node_modules/yourmodule/bin, which is probably not whatyou want. app-root-path is aware of this edge-case and will strip the /bin automatically.

Change Log

3.1.0

  • Added TypeScript types
  • Added fallback for when require.main is missing (ESM imports)

3.0.0

  • Improved Yarn Plug'n'Play support
  • Fixed bug when used with webpack

2.2.1

  • Better handling of webpack

2.2.0

  • Added support for Yarn Plug'n'Play
  • Adjusted browser-shim to address webpack warnings
  • Bumped minimum Node version to 6

2.0.1

  • Minor tweaks to how electron-specific logic runs. Should help with packagers that try to resolve all require() statements during packaging.

2.0.0

  • Removed official support for node < 4.0
  • Removed support for passing module.require to appRootPath.require (which has been deprecated for a while)
  • Implemented semantic-release from here on out
  • Added browserify-compatible shim

1.3.0

  • Updated electron to match changes in version 1.0 of that project

1.2.1

  • Had to bump package version because 1.2.0 got published to npm as @beta

1.2.0

  • Special logic to resolve correctly when in an electron renderer process

1.1.0

  • Special logic to handle an edge case when used in a globally-installed CLI project
  • Fixed a bug where setPath() did not update require('app-root-path').path
  • Moved some logic outside of the resolve() function so that it's not called multiple times

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

Development Nodes

When using semantic-release, the preferred methodfor commits is:

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

app-root-path (2024)
Top Articles
Department of Adulting - Your Easy Step-by-Step Guide to Opening an Investment Account
Make Money from Home - 50 Ideas from Thrifty Guardian
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Cheryll Lueilwitz

Last Updated:

Views: 6397

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Cheryll Lueilwitz

Birthday: 1997-12-23

Address: 4653 O'Kon Hill, Lake Juanstad, AR 65469

Phone: +494124489301

Job: Marketing Representative

Hobby: Reading, Ice skating, Foraging, BASE jumping, Hiking, Skateboarding, Kayaking

Introduction: My name is Cheryll Lueilwitz, I am a sparkling, clean, super, lucky, joyous, outstanding, lucky person who loves writing and wants to share my knowledge and understanding with you.