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-alias

v1.0.5

Published

Path manager for Node.js

Downloads

657

Readme

Coverage Status Codeship Status for manuelvulp/require-alias Alias logo

[Node.js] Alias

Library that might help you deal with Node paths

npm install require-alias

Table of Contents

#Quick example

(Of how one might use it in project)

Simple application with three files and some folders with structure like:

C:\test - the application folder

C:\test\this\is\an\example.js - random export of function that returns string 'foo'

module.exports = function () {
    return 'foo';
};

C:\test\app.js - the main runnable where to configure alias

var Alias = require('require-alias');

global.alias = new Alias({
    aliases: {
        '@root': './', // will point to C:\test\
        '@folder': 'this/is/an/' // will point to C:\test\this\is\an\
    }
});

alias.require('@root/random');

C:\test\random.js - just a random file to test alias

// Can be done in any file as long as aliases are defined first and assigned to global
var path = alias.path('@folder/example');
console.log(path); // Output: C:\test\this\is\an\example

var module = alias.require('@folder/example');
console.log(module()); // Output: foo

#Full example

/**
 * Require and configure Alias with initial options
 */

var Alias = require('../src/require-alias');

var exampleAlias = new Alias({

    root: './app/',

    aliases: {
        '@models': 'models',
        '@random': 'random',
        '@another': 'another/path',
        '@and': 'another/path'
    }

});

/**
 * Add some more aliases
 */

exampleAlias.add({
    '@handlers': 'handlers',
    '@bar': 'models/bar'
});

/**
 * Add single alias
 */

exampleAlias.add('@bar', 'models/bar');

/**
 * Delete single alias
 */

exampleAlias.delete('@random');

/**
 * Delete multiple aliases
 */

exampleAlias.delete(['@another', '@and']);

/**
 * Get path to foo using alias + path
 */

var pathToFoo = exampleAlias.path('@handlers/for/foo');
var foo = require(pathToFoo);
console.log(foo()); // Output: Foo

/**
 * Get path to bar with using only alias
 */

var pathToBar = exampleAlias.path('@bar');
var bar = require(pathToBar);
console.log(bar()); // Output: Bar

/**
 * Require module using alias
 */

var moduleFoo = exampleAlias.require('@handlers/for/foo');
console.log(moduleFoo()); // Output: Foo

/**
 * This is the basic usage.
 *
 * You can also use it in other ways to make your life easier. Say you don't like
 * the functionality of default require and do not wish to write most of the time
 *
 *      require(alias('@foo/bar'))
 *
 * You may assign alias to global variable and use it instead of require:
 */

global.alias = exampleAlias;

/**
 * Just an example
 */

alias.add('@baz', 'baz');
var baz = alias.require('@baz');
console.log(baz()); // Output: Baz

var pathToBaz = alias.path('@baz');
console.log(pathToBaz); // Output: C:\require-alias\example\app\baz (Or wherever
                        // your project is located

/**
 * Note
 *
 * Whether you assign alias to global variable or not is up to you. Even though it is
 * highly recommended (and also a good practice) to not assign anything to global
 * variables, this might be a good candidate for global scope. The intent of this
 * "helper" was to make requiring modules and paths easier, maybe even replace the
 * majority of cases where "require" is used.
 *
 */

#API

var alias = new Alias();

alias.root.set(String)

  • String - New root path

alias.root.get()

  • Returns current root

alias.storage(String)

Returns path of given alias or the whole storage

  • String (optional) - Alias (eg '@foo')

alias.add(String, String)

Add single alias

  • String - Alias (eg '@foo')

  • String - Path reference of alias (eg '../folder/another/folder')

alias.add(Object)

Add single / multiple alias(es)

  • Object - Key-value object where key is alias and value is path reference alias (eg {@foo: 'foo/bar'})

alias.delete(String)

Deletes single alias

  • String - Alias (eg '@foo')

alias.delete(Array)

Deletes multiple aliases

  • Array - Array of strings, each being alias (eg ['@foo', '@bar'])

alias.path(String)

Returns path reference of alias

  • String - Alias (eg '@foo')

alias.require(String)

Returns module export of alias

  • String - Alias (eg '@foo')