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

esm-module-alias

v2.2.1

Published

An alternative to module-alias, but for esm

Downloads

19,841

Readme

Commitizen friendly License GitHub issues GitHub stars npm

esm-module-alias

An alternative to module-alias, but for esm

Project purpose

The purpose of this project is allowing developers that use esm modules to have a feature similar to the one provided by module alias.

module-alias provides the possibility to alias modules to a different path, taking the same example that is used in its documentation:

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

can become:

import module from '@deep/module';

To allow this, one should add some paths to the package.json, like:

{
    "aliases": {
        "@deep": "src/some/very/deep"
    }
}

The module stopped working after the introduction of the esm in NodeJS. In addition, at the moment in which this README was written, module-alias was last published three years ago.

How to pass to esm

Taken from this fantastic guide:

  • Add "type": "module" to your package.json.
  • Replace "main": "index.js" with "exports": "./index.js" in your package.json.
  • Update the "engines" field in package.json to Node.js 14: "node": ">=14.16". (Excluding Node.js 12 as it's no longer supported)
  • Remove 'use strict'; from all JavaScript files.
  • Replace all require()/module.export with import/export.
  • Use only full relative file paths for imports: import x from '.';import x from './index.js';.
  • If you have a TypeScript type definition (for example, index.d.ts), update it to use ESM imports/exports.
  • Optional but recommended, use the node: protocol for imports.

How to use this module to continue using the module aliases

To use this module:

  • Install the module by exeuting
    $ npm install esm-module-alias
  • Add the property aliases to the package.json, the same way you would have done with module-alias, for example:
    {
      "aliases": {
          "@root"      : ".",
          "@deep"      : "src/some/very/deep/directory/or/file",
          "@my_module" : "lib/some-file.js",
          "something"  : "src/foo"
      }
    }
  • When you execute your script, add this module as a loader by adding --loader esm-module-alias/loader, for example:
    node --loader esm-module-alias/loader --no-warnings myscript.js # Note that --no-warnings is to disable a warning and is optional

An option if you want to create a custom loader

You can also create a custom loader, because the library exports a function that given an object like the aliases one that one would define in the package.json, will return a function that will be used as a loader.

To do so:

  • Create a custom file named as you want, for instance my-loader.mjs:
    import generateAliasesResolver from 'esm-module-alias'; 
    const aliases = {
      "@root": ".",
      "@deep": "src/some/very/deep/directory/or/file",
      "@my_module": "lib/some-file.js",
      "something": "src/foo"
    };
    export const resolve = generateAliasesResolver(aliases);
  • When you execute your script, add that script as a loader by adding --loader ./my-loader.mjs, for example:
    node --loader=./my-loader.mjs --no-warnings myscript.js # Note that --no-warnings is to disable a warning and is optional

What if you want to change the matching behaviour?

You can also have a custom matcher, a function that customize the behaviour of a path matching an alias.

To do so:

  • Create a custom file named as you want, for instance my-loader.mjs:
    import generateAliasesResolver from 'esm-module-alias'; 
    const aliases = {
      "@root": ".",
      "@deep": "src/some/very/deep/directory/or/file",
      "@my_module": "lib/some-file.js",
      "something": "src/foo"
    };
    const matcher = (path, alias) => {
      return (path.indexOf(alias) === 0); // Your customized code
    }; 
    export const resolve = generateAliasesResolver(aliases, { matcher }); // The custom matcher is passed to the options
  • When you execute your script, add that script as a loader by adding --loader ./my-loader.mjs, for example:
    node --loader=./my-loader.mjs --no-warnings myscript.js # Note that --no-warnings is to disable a warning and is optional

Tests

Tests are run with Jest and work by executing npm test with shelljs on a bunch of sample projects,

Note

This package took inspiration from a comment of a Github issue