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 🙏

© 2025 – Pkg Stats / Ryan Hefner

best-require

v1.1.4

Published

A require() hook plugin for requiring a module in your project elegantly for Node.js

Readme

Build Status NPM version

Best Require

Best Require is a require() hook plugin for requiring a module in your project elegantly for Node.js.

require(':controllers/posts');
require(':models/Users');
require('~/application/apis/config');

Installation

npm install best-require --save

Introduction

Background

Have you ever coded like this in your application?

// require the 'posts' module
require('./posts');
require('./controllers/posts');
require('../controllers/posts');
require('../../controllers/posts');
require('../../../apis/controllers/posts');

When our project contains many layers of directory, the relative path of each module will become complicated, which not only makes us very confused, but also makes the project difficult to maintain.

When faced with the problem, you might tend to find a unified way to access the posts module, just like me. I used to require modules in this way:

require(ROOT_PATH + '/application/apis/controllers/posts');
// other require()...
require(ROOT_PATH + '/application/apis/controllers/users');
require(ROOT_PATH + '/application/apis/controllers/products');
require(ROOT_PATH + '/application/apis/services/rest');
require(ROOT_PATH + '/application/apis/config');

ummmm... It's more maintainable than before. But, ROOT_PATH is ugly, isn't it?

Solution

Let's try to use Best Require, by adding this at the beginning of the app:

require('best-require')(process.cwd())

Now, we can use ~ to represent process.cwd() in the path:

require('~/application/apis/controllers/posts');
require('~/application/apis/controllers/users');
require('~/application/apis/controllers/products');
require('~/application/apis/services/rest');
require('~/application/apis/config');

However, this directory name is still a bit long, which can be shortened by defining the name mapping:

const ROOT_PATH = process.cwd();
require('best-require')(ROOT_PATH, {
    apis: ROOT_PATH + '/application/apis',
    controllers: ROOT_PATH + '/application/apis/controllers'
});

Then we are able to use :apis for ~/application/apis and :controllers for ~/application/apis/controllers:

require(':controllers/posts');
require(':controllers/users');
require(':controllers/products');
require(':apis/services/rest');
require(':apis/config');

With the release V1.1+, you can also use other keys in the definition of a key-value pair in the name mapping, and our plug-in will automatically handle the keys' dependencies on each other. Therefore, the definition can be simplified as:

require('best-require')(ROOT_PATH, {
    apis: '~/application/apis',
    controllers: ':apis/controllers'
});

Usage

Add this at the beginning of the program:

require('best-require')(
    root_path, // [optional] project root directory, defaults to `process.cwd()`
    name_mapping // [optional] name mapping
)();

Then you can use:

require('~/(path)'); // require(root_path + '/(path)');
require(':key/(path)'); // require(name_mapping[key] + '/(path)');