Home > Net >  Is there any possibility to avoid multiple ../ when we are importing a function?
Is there any possibility to avoid multiple ../ when we are importing a function?

Time:06-03

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!

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

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')
  • Related