Skip to content
Advertisement

Built-in nodejs path aliasing and implied index.js module resolution

I want to set up import path aliasing in a typescript/node/express project WITHOUT using other packages like tsconfig-paths and link-module-alias. I would like to do this with built-in nodejs functionality (the Typscript aliasing is already done).

Basically:

// Change
import { myFn } from '../../../utils';

// To
import { myFn } from '#this/utils';

The glimmer of hope I have is with node’s subpath imports. Theoretically, I can just add this to my package.json:

"imports": {
    "#this/*": "./dist/*"
}

Here’s the problem

This works for explicitly importing the *.js files, but it does fails for implied index.js (i.e. Folders as modules)

// Works
import { myFn } from '#this/utils/index.js';

// Does not work
import { myFn } from '#this/utils';

However, I would expect node to resolve the import like this:

  • if utils is a directory, the import should resolve to */utils/index.js
  • if utils is a file, the import should resolve to */utils.js

My first thought would be to update the package.json imports to include all possibilities, but this is not supported:

"imports": {
    "#this/*": [
        "./dist/*",
        "./dist/*.js",
        "./dist/*/index.js"
    ]
}

The other option seems to be to define an exports entry for every directory, but that is not scalable.

Advertisement

Answer

With --experimental-specifier-resolution=node flag the code below works for me.

index.js:

import { myFn } from '#this';

package.json:

...
  "imports": {
    "#this": "./utils"
  }
...
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement