Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

index.js:

import { myFn } from '#this';

package.json:

...
  "imports": {
    "#this": "./utils"
  }
...
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!