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

require-at

v1.0.6

Published

Call require pretending your are at another directory

Downloads

2,239,540

Readme

NPM version Build Status Dependency Status devDependency Status

require-at

Allow you to call require or require.resolve pretending that you are at another directory.

Purpose

Given the directory structure below with two NodeJS apps:

app1
|-+ foo
| +-- index.js
| +--+ node_modules
|    +--+ x 
|       + ...
app2
|-+ bar
| +-- index.js
| +--+ node_modules
|    +--+ y
|       + ...

When you call require("x") in /app1/foo/index.js, NodeJS will search and find module x there.

Now from the same file, if you want to resolve the module y under the directory /app2/bar, you have to use an absolute or relative path directly pointing to y, and you may have to do some searching, probably re-implementing Node's module searching algorithm if you don't know exactly where y could be.

However, in the file /app2/bar/index.js, it can just do require("y") and Node would automatically find the module for it, because that file is at the location where y is under.

What if from the file /app1/foo/index.js, you can call require as if you were at the directory /app2/bar, then you would be able to utilize Node's module searching automatically.

To achieve this, most other implementations choose to re-implement Node's module searching algorithm.

This module's approach is to tap into Node's module and let it do the work.

Install

$ npm install require-at --save

Usage

A single function is exported.

requireAt(dir, [request])
  • If you call it with just dir, then it returns a require function that's been binded to the directory dir. You can use it to load any module as if you are at dir.
    • You can also call require.resolve with the same effect.
  • If you call it with dir and a request, then it will load and return the module request as if at dir.
Example
const requireAt = require("require-at");

// get back a require binded to /another/dir

const requireAtAnother = requireAt("/another/dir/");
const modXPath = requireAtAnother.resolve("modX");
const modX = requireAtAnother("modX");

// load modY at /another/yet/dir directly

const modY = requireAt("/another/yet/dir", "modY");

License

Apache-2.0 © Joel Chen