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

path-alias

v0.0.12

Published

Path aliases, root path, module loader

Downloads

65

Readme

path-alias

This module allows you to use path aliases in your NodeJS modules and client-side (in browser).

The problem

Probably In a big project you will have something like this:

require('../../../packages/user/models/role') - relative paths

Even if you use a root path detector:

var pathFromRoot = require('some-module-to-detect-root')
require(pathFromRoot('app/packages/user/models/role'))

It is a mess! Imagine that you need to rename/move some file or directory, you need to change each of these paths to point to the new location.

How to solve it?

Use path-alias!

var pathAlias = require('path-alias');

//setup alias:
pathAlias.setAlias('user', 'app/packages/user');

//require module:
var role = pathAlias('@user/models/role');

You can also use variables in any parts:

pathAlias.setAlias('c', 'client-side', false);

// will require file: app/packages/user/models/some.client-side.js
var clientModel = pathAlias('@user/models/some.@c');

Also you can use this module to require root-related files:

// will require {PROJECT_ROOT}/app/myModule
var someModule = pathAlias('app/myModule');

Methods

Calling with argument - will resolve path, require it and return a module.

var pathAlias = require('path-alias');

//with aliases
var myModel = pathAlias('@user/models/myModel');

//just add root prefix
var anotherModel = pathAlias('app/modules/myModule');

getRoot()/setRoot()

var pathAlias = require('path-alias');
//root path will be detected
console.log(pathAlias.getRoot());

The directory with package.json is considered a project's root directory.

You can also set custom root-path:

pathAlias.setRoot('/you/custom/path')

setAlias/setAliases/getAliases

pathAlias.setAlias('alias', 'path or suffix', resolve);

resolve - should path be resolved or not. True by default.

How does it work?

  • './path/' - path, related to caller location will be transformed to absolute
  • 'some/path' - root path will be added to non absolute path

If you want to add file suffix you will not want to resolve this alias:

pathAlias.setAlias('c', 'client-suffix', false);

setAliases/getAliases

pathAlias.setAliases({
	'user' : 'app/packages/user'
});

resolve

Resolve path and return it:

var path = pathAlias.resolve('@user/models/myModel');

exportAliasesForClientSide

It needs to export aliases for client-side module.

Using client-side

  1. Add pathAlias loader to your webpack config. It replaces all pathAlias('@some/path.@c') entries to require('/resolved/some/path.client'). Path will be resolved before passing it to require().
const aliasLoader = {
	loader: path.resolve('node_modules/path-alias/webpack/loader.js'),
	options: {
		test: (filePath) => {
			//some custom logick to include/exclude files from loader.
		},
	}
}
  1. In your client side, if you want to use pathAlias for dynamic require, you need to pass require.context to pathAlias:
const contextRequire = require.context('./app', true, /widgets\/[^\/]+\.client\.(js|coffee)$/i);
const pathAlias = require('path-alias');

pathAlias.setAliases({
	'c': 'client',
	'widgets': './widgets'
});

pathAlias.setRequireCallback((filePath, resolved) => {
	if (!/\.(js|coffee)$/i.test(resolved)) {
		let foundExt = null;
		let found = contextRequire.keys().find((item) => {
			if (`${resolved}.js` == item) {
				foundExt = 'js';
				return true;
			}

			if (`${resolved}.coffee` == item) {
				foundExt = 'coffee';
				return true;
			}
		});

		if (found)
			resolved += `.${foundExt}`;
	}

	return contextRequire(resolved);
});