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

groot

v1.0.0

Published

Module loader with global variables

Downloads

6

Readme

groot

Module loader with global variables

npm travis coveralls

This module tries to solve and improve the module loading. It's very common to have require()'s with relative paths like ../../../foo/bar.

This is a well-known problem known by the Node.js community: Better local require() paths for Node.js. There are some solutions that seem to work but I personally dislike most of them, especially the one which uses the node_modules directory to store the modules of your app. My advice is to only use node_modules for external modules, never for storing you own modules.

The way this module avoids the relative paths is by using global variables. This is by far the best solution to this problem; clean, easy to understand and compatible with all the operating systems.

require('groot')({ requireVar: '__require', rootVar: '__root' });

If you execute the above piece of code in the main file, which is typically stored in the root directory, __root and __require will be set as global variables. __root will contain the absolute path of the root directory, and __require will be a function similar to require() but for loading the modules relative to the root directory.

By default, the root directory is the fiel's __dirname of the caller.

For example, given the project directory tree:

.
├─ app.js
├─ foo
│  └─ bar.js
└─ baz
   └─ qux.js
// app.js
require('groot')({ requireVar: '__require', rootVar: '__root' });

// bar.js
var qux = __require('./baz/qux');
// "." points to the __dirname of app.js, that is, the project's root directory

Note that __require('baz/qux') (without a dot .) is intentionally invalid and will throw an error for avoiding confusion with modules stored inside node_modules. The path must begin with a dot, ..

An extra option can be specified to manually set the root directory:

require('groot')({
  requireVar: '__require',
  rootVar: '__root',
  rootDir: __dirname
});

By setting rootDir to __dirname, this is, in fact, the same as not setting the rootDir option.

module([options]) : undefined

Options:

  • requireVar - String
    Name of the global variable to require modules.
  • rootVar - String
    Name of the global variable of the root directory.
  • rootDir - String
    Absolute path that will be used as the root directory.