Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

88
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda