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

better-module-alias

v1.2.1

Published

An improved version of the fantastic module-alias package designed to include support for package linking in `node_modules/`.

Downloads

1,523

Readme

better-module-alias is an improved version of the fantastic module-alias package.

Better Module Alias

Fix the issue of having to do relative paths like:

require('../../../../some/deep/module')

Instead, make your code look like:

require('$utils/some/deep/module')

How do I use it?

package.json

This package uses the same package.json formatting as module-alias.

In your package.json, add a _moduleAliases object formatted like this:

{
  "_moduleAliases": {
    "$tests": "./tests",
    "$utils": "./utils"
  }
}
  • NOTE: Prefixing with $ is considered a best-practice so it doesn't interfere with @-scoped npm packages.
  • NOTE: We prefix a $ so it's obvious which imports are from npm packages and which come from better-module-alias.

Node.js

NOTE: better-module-alias has to be imported before any $ imports in your root code file, or it won't work.

If you want to use better-module-alias in Node.js, you'll need to import this package at the top of the file that gets run in your node command such as ./index.js.

require("better-module-alias")(__dirname);

// or

import betterModuleAlias from "better-module-alias";
betterModuleAlias(__dirname);

When you want to require an aliased file, do it like so:

const someModule = require("$utils/someModule");

// or

import someModule from "$utils/someModule";

An alternative way is to call the betterModuleAlias function, and pass the aliases as the second argument.

// Method 1 (using package.json _moduleAliases)
import betterModuleAlias from "better-module-alias";
betterModuleAlias(__dirname);

// Method 2 (using the second argument as aliases)
require("better-module-alias")(__dirname, {
  $tests: "./tests",
  $utils: "./utils",
});

Examples

Will work:
// index.js
require("better-module-alias")(__dirname);
const someModule = require("$utils/someModule");
Won't work:
// index.js
const someModule = require("$utils/someModule");
require("better-module-alias")(__dirname);

Issues with fs

Libraries like fs won't work with better-module-alias because it's overriding the require functionality of CommonJS, not file lookups in other libraries. The way to get around that is by using require.resolve.

Where you might have done:

fs.readFile("../../../../some/deep/module", callback);

You can use require.resolve to do this instead:

fs.readFile(require.resolve("$utils/some/deep/module"), callback);

That'll allow you to get the same functionality without having to have 2 ways of writing the same code.

Webpack

Webpack has a built in support for aliases and custom modules directories.

Add this code to your Webpack config to get it working with better-module-alias:

const packageJson = require("./package.json");

const webpackConfig = {
  // ...
  resolve: {
    alias: {
      ...packageJson._moduleAliases,
    },
  },
  // ...
};

FAQ

Why this version?

Because I fixed an issue inherit in the original module-alias where you can't use npm link or yarn link with other packages also using module-alias. This also fixes the issue where you might be using module-alias in your main package, but also one in node_modules/.

Why not make a PR in the original module-alias?

I did, and it wasn't accepted.

module-alias works by also going into subdirectories and linking up packages in node_modules as well when linking.

Instead of going through that,

What about app-module-path?

I used to use app-module-path in the past and suffered less issues than module-alias, but it also didn't do what I needed. Instead, I created this library.