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

getmod

v1.0.1

Published

Module loader with aliases

Downloads

3

Readme

getmod

Module loader with aliases

npm version 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.

I'm not going to use this module. This is just a proof of concept. The cleanest way to solve the relative paths is by prefixing them with the __root variable. In fact, in my opinion Node.js should consider adding it, as it's perfectly aligned with the __filename and __dirname approach.

The way this module avoids the relative paths is by using more relative paths that shorten the paths and make them relative from anywhere. Think about them as marks, aliases, checkpoints, etc.

Marking a directory

Suppose you have this tree structure:

.
├ a
| └ b
|   └ c
|     └ file1.js
├ d
| └ e
|   └ f
|     └ file2.js
└ app.js

From file1.js if you need to load file2.js, the path is quite long: require('../../d/e/f/file2'). What about making the relative paths relative from .? Put a mark named my-mark pointing to the directory d:

// app.js
global.mod = require('getmod')();

mod.mark({
  'my-mark': 'd'
});

// file1.js
var f = mod('my-mark/e/f/file2');

Instead of my-mark, name the mark as d and you'll have a very descriptive path: mod('d/e/f/file2').

Marking a file

Marks can also point to a file. For example, you have a module you'd like to load with a very short path because it's pretty important, like a module that tells the status of your server:

mod.mark({
  // The ending ".js" is optional, like in require()
  status: 'path/to/the/status/module'
});

// Anywhere in your app
var status = mod('status');

Replacing require()

You can also use this library to load:

  • Any file without marks.
  • External and core modules.

Therefore, you can completely replace the require() function like so:

//The very first line in the entry point of your app
global.mod = require('getmod')();

Now you can use mod() from anywhere instead of require(). In fact, this is the way to use this library.

Functions


module() : Function

Creates a new namespace for the marks.

var mod = require('getmod')();

mod(name) : Any

Loads the module. It returns whatever is exported, just like the built-in require() function. Paths can be any of these:

  • Mark: mod('my-mark/file.js')
  • System path: mod('./a/b/c/file.js')
  • External module: mod('express')
  • Core module: mod('fs')

If the module name has a mark and is the same as any core module, ie: util, the core module will be loaded, just the same behaviour as with external modules with the same name as a core module.

mod.mark(marks) : undefined

Puts marks that point to relative paths. It takes the directory of the current file the reference from which the paths are relative. It can be called multiple times.

//cwd = /foo/bar

// ./file1.js
mod.mark({
  c: 'a/b/c'
});

// ./random/path/file2.js
mod.mark({
  f: 'd/e/f'
});

//Anywhere in your app
mod.resolve('c'); // /foo/bar/a/b/c
mod.resolve('f'); // /foo/bar/random/path/d/e/f

mod.resolve(name) : String

Returns the absolute path of the module, just like the built-in require() function, but it also resolves marks.

//cwd = /foo/bar

mod.mark({
  file: 'a/b/c/file.js'
});

mod.resolve('file'); // /foo/bar/a/b/c/file.js

module.resolveLoose(name) : String

Same as resolve() but without any restrictions. The name can be anything including marks, even if the path doesn't exist it is still resolved.

//cwd = /foo/bar

mod.mark({
  file: 'a/b/c/file.js'
});

mod.resolveLoose('file'); // /foo/bar/a/b/c/file.js
mod.resolveLoose('./a/b/c/file'); // /foo/bar/a/b/c/file
mod.resolveLoose('a/b/c/file'); // /foo/bar/a/b/c/file
mod.resolveLoose('random/path'); // /foo/bar/random/path