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

94
Views
Is there any possibility to avoid multiple ../ when we are importing a function?

I want to avoid this thing at importing a function

const ApiError = require('../../../../classes/ErrorClass');

Is any possibility to use,I don't know Ex:

require('mypath/ErrorClass')

If I have 11 imports I don't want to have something like this

const ApiError = require('../../../../classes/ErrorClass');
const ex= require('../../../classes/ApiClass');
const ex1= require('../../../../utils/utils');
const ex2= require('../../../etc/etc');
const ex3= require('../../../../../etc/etc');
const ex4= require('../../../../etc/etc');
const ex5= require('../../../../etc/etc');
const ex6= require('../../../../etc/etc');

Thank you!

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

0

Yes, you can do it with package.json. Imagine you have the following structure:

my-app/
├─ src/
│  ├─ index.js
│  ├─ node_modules/
│  ├─ dir/
│  │  ├─ nested/
│  │  │  ├─ nestedMod.js
│  ├─ function/
│  │  ├─ myMod.js
│  │  ├─ myMod3.js
│  │  ├─ myMod2.js
├─ package.json

You have to add the imports field in package.json

{
  "name": "my-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "imports": {
    "#nestedMod": "./src/dir/nested/nestedMod.js",
    "#myFunc": "./src/function/myMod.js",
    "#myFunc2": "./src/function/myMod2.js",
    "#myFuncs/*": "./src/function/*.js"
  }
}

then you can do:

const nestedMod = require('#nestedMod')
const myFunc = require('#myFunc')
const myFunc2 = require('#myFunc2')
const myFunc3 = require('#myFuncs/myMod3')

P.S.: Only works in Node

about 4 years ago · Juan Pablo Isaza Report

0

Since there's no insight on your configuration, I'll try to provide some different solutions here.

Webpack

If you're using webpack, you could use an alias config for the resolve property to create a mapping between your actual path the what you want to call it. It would look something like this:

const path = require('path');

module.exports = {
  resolve: {
    alias: {
      Components: path.resolve(__dirname, '/path/to/function') 
    }
  }
}

Check the resolve chapter form webpack docs for more info.

Typescript

While using typescript, you could specify the baseUrl property in the tsconfig.json, which would allow you to specify every import relative to the path you specified there. Let's say you have this path: '../../../../classes/ErrorClass', which, expanded, could look like: src/some/folder/structure/classes/ErrorClass. After the config, this could look like this classes/ErrorClass, if the baseUrl is configured to src/some/folder/structure.

about 4 years ago · Juan Pablo Isaza Report

0

you would want to use a built-in variable node provided __dirname to have the app root directory

say you have this directory

myapp/
├─ utils/
│  ├─ main.js
├─ lib/
│  ├─ mylib.js
├─ node_modules/

so, main.js:

const mylib = require(__dirname + '/lib/mylib')
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!