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

373
Views
Importing files in node package without including dist folder name

I have following directory structure for a library which will be imported by other node js project.

my-lib/
├─ dist/
│  ├─ index.js
│  ├─ foo.js
├─ src/
│  ├─ index.ts
│  ├─ foo.ts
├─ package.json

I have following package.json

{
  "name": "my-lib",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js"
}

I have specified main as dist/index.js, so if I understand correctly, members exported from index.ts (js) can be imported as import abc from 'my-lib'. If I have to access exported members from foo.ts (js) file then I might end up doing import foo from 'my-lib/dist/foo'. So here I have to specify the dist folder name in import path. Is there way to specify just 'my-lib/foo' omitting dist folder name? (just like importing dist/index.js file.)

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

This can be accomplished with subpath exports in package.json.

  "main": "./dist/index.js",
  "exports": {
    ".": "./dist/index.js",
    "./foo": "./dist/foo.js"
  }
over 4 years ago · Santiago Trujillo Report

0

As explained in node.js documentation, it is possible to add subpath exports. Check it out here.

In your case it should look similar to following

{
  "name": "my-lib",
  "version": "1.0.0",
  "description": "",
  "main": "./dist/index.js",
  "exports": {
    ".": "./dist/index.js",
    "./foo": "./dist/foo.js"
  }
}

After exporting like this you can import as follows

import foo from 'my-lib/foo'
over 4 years ago · Santiago Trujillo 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!