'Node Subpath Imports External Directory

In Vite/WebPack, we can define path aliases fairly easily, and we can even use the @ to define these. However, in an ESM Node app, it's not as easy. There's module-alias, but it's a little outdated, and doesn't work for ESM packages. All of this led me to Subpath Imports.

Subpath Imports seem to work just fine (except I can't use @ instead of #) with files/directories in the package.json's directory, but it doesn't seems to work with directories outside. e.g. this is my imports config in package.json:

"imports": {
  "#api/*": "./*",
  "#shared/*": "../shared/*"
}

The #api works properly, but the #shared does not. Is there some sort of extra config that I'm unaware of, or is this not possible with Subpath Imports? I know the docs say it is possible to define internal package import maps that only apply to import specifiers from within the package itself, so if it's not possible, what are my alternatives?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source