Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

87
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda