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

shrew

v0.1.1

Published

Extract root path from a module in an NPM context (scripts)

Downloads

321

Readme

Shrew :mouse2:

This module has only one function and it returns the root path of the current main module when using with NPM.

const shrew = require ('shrew');
const root = shrew ();

It's very useful in the case where you have a dependency (a module in node_modules/) and you want to retrieve the main module path. More precisely the module which is running a script with NPM.

The main module is not always located in the parent ../ because you can't be 100% sure that there is only one level of node_modules/. Since node >=5, it seems that all modules are placed in the root node_modules directory. But it's possible that some modules are nested because it relies on uncompatible versions of the same module (for example, two different major versions). An other case is the use of npm link where the modules are always nested.

Fine, how to use it?

In your package.json file, sometimes you want to call a command in a postinstall script (for example). Or any other scripts even custom scripts.

{
  "scripts": {
    "postinstall": "my-super-command"
  }
}

This command is deployed in your ./node_modules/.bin directory and uses some other modules like ./node_modules/my-super-command/node_modules/one-lib. The module one-lib wants to change something in the root directory of your module.

In this case, one-lib can not just use the parent directory ../ in order to retrieve the main module. It's here that Shrew is useful. The one-lib module must use shrew () in order to retrieve the path on the module where the postinstall script is called.

Why it's working only with NPM?

You can't use Shrew without NPM because this library uses an environment variable set by NPM. Without this variable, it's not possible to retrieve the main module.