Skip to content

Implement #hash import resolution for package.json imports#148

Open
larsmoa wants to merge 2 commits intodependents:mainfrom
larsmoa:larsmoa/nodejs-imports-support
Open

Implement #hash import resolution for package.json imports#148
larsmoa wants to merge 2 commits intodependents:mainfrom
larsmoa:larsmoa/nodejs-imports-support

Conversation

@larsmoa
Copy link

@larsmoa larsmoa commented Dec 17, 2025

Summary

Adds support for resolving #hash imports defined in the imports field of package.json.

Motivation

This change will enable e.g. madge to properly resolve #hash-style imports. See pahen/madge#406.

Currently, imports like:

import { openAggregate } from "#aggregate";
import { Button } from "#lib/button";

...fail to resolve when the package.json contains:

{
  "imports": {
    "#aggregate": "./src/util/aggregate.js",
    "#lib/*": "./src/lib/*.js"
  }
}

This causes madge to skip these dependencies entirely, making dependency graphs incomplete for projects using Node.js subpath imports.

Solution

This PR adds hash import resolution by leveraging enhanced-resolve (already a dependency) which fully supports the Node.js subpath imports specification including:

  • Exact matches (#utils./src/utils.js)
  • Wildcard patterns (#lib/*./src/lib/*.js)
  • Conditional imports (import, require, node, default conditions)
  • Nested conditions

References

@larsmoa larsmoa marked this pull request as ready for review December 17, 2025 13:56
@XhmikosR XhmikosR force-pushed the larsmoa/nodejs-imports-support branch from a99ccf4 to af9b687 Compare February 28, 2026 16:46
@XhmikosR
Copy link
Member

I could use more eyes on this PR...

@markmssd
Copy link

markmssd commented Mar 2, 2026

Hoping to see this PR land soon, so we can leverage it in Madge! Btw, in our case, the imports config looks like this:

  "imports": {
    "#utils/*": {
      "types": [
        "./*.tsx",
        "./*.ts",
        "./*.js",
        "./*/index.ts"
      ],
      "default": "./*"
    }
  },

@larsmoa would it be supported?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants