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

resquire

v1.1.1

Published

Allows for more complex requires, making Node requires feel more like importing with namespaces as seen in other languages.

Downloads

8

Readme

resquire

This npm module allows the user to perform more advanced requires, functioning similarly to namespaces as seen in other programming languages.

Installation

$ npm install resquire

Usage

Before we begin, resquire needs to be required: preferably from the root file of an application or module, as as soon as it has been required, its functionality is available from everywhere.

require('resquire')

Paths Relative to Root

First and foremost, resquire is slightly inspired by rooty and uses a similar syntax for requiring files relative to the root. Being able to require files relative to the root saves us the nightmare of having to manually type in relative paths.

The default require behavior remains the same, whereas all require paths are relative to the file they are required from:

const utils = require('../../../../../utils')

Beyond doubt, this is rarely a prefered behavior. By utilizing the '^' (caret) character, files can be required as relative from the root in resquire:

const utils = require('^utils')

The two examples above do the exact same thing (granted we are actually nested five levels in away from the root), but the second one is by all means quicker and less painful.

Requiring Multiple Modules at Once

The second, more unique feature resquire has to offer, is that multiple modules can be required at the same time, given they are located in the same directory.

This feature is especially useful if one decides to use the '^' (caret) syntax for all requires at all times (thus maintaining consistency).

Through the use of pattern matching, and a curlybrace-styled array in the path sent to the require function, multiple modules may be loaded and utilized at the same time:

const [hello, goodbye] = require('^greetings/{hello, goodbye}')

The array returned is filled from left to right from the inputs acquired in the curlybrace-styled array from the string. Since it returns an array, the names for the pattern matched values do not have to coincide with the names of the files.

Furthermore, this feature can be nested:

const [hello, goodbye, bye] = require('^greetings/{sincere/{hello, goodbye}, insincere/bye}')

As such, all files needed can theoretically be required at the same time, even though it most certainly would make a mess of things.

The braces can be used with a require that does not use the '^' (caret) feature.

Modifying the Root Directory

Sometimes it might be necessary to change the root directory that the ^ (caret) operator requires files from: for instance when writing tests, it might be better to separate the files so that source code goes under a lib/ directory whilst tests go under a test/ directory.

Normally, the root of the project would be where the package.json file is located:

const file = require('^file')

On the other hand, if that file we are trying to require is located under the lib/ directory, we can go ahead and add a file called resquire.json to the root of our application (next to the package.json file):

{
	"root": "lib"
}

Using the require function as above, the file will be required as ^lib/file rather than as ^file.

License

This module, and the code therein, is licensed under ISC.