Joshua Galit
Joshua Galit

Reputation: 1456

Is there a way to shorten the import location in Next.JS

I am traditionally came from Vue and i kinda like to have the same import { } from '@/whatever and the location seems like having many dot dot import {} from './../../from/source/file

enter image description here

Please let me know if you have any installation during this kinda process

Upvotes: 0

Views: 1339

Answers (3)

Joshua Galit
Joshua Galit

Reputation: 1456

Thank you for you answer i kinda like to have ~/file/file so i created this short configuration

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "~/*": ["src/*"]
    }
  }
} 

Upvotes: 0

Gh05d
Gh05d

Reputation: 8982

You can do this by editing the paths property the jsconfig.json. If you would like to have a shortcut for graphql, you could do this for example:

// jsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "graphql/*": ["graphql/*"]
    }
  }
}

Your import statement would so shorten to this:

import { ADD_USER_MUTATION } from "graphql/mutations"
import { GET_USER_BY_EMAIL_QUERY } from "graphql/queries"

This only works if on the graphql folder is in the root directory. Read more about this here.

Upvotes: 2

enoch
enoch

Reputation: 3123

Yes it's possible to do that in Next.js, it is explained in the documentation and no installation during the process

Upvotes: 4

Related Questions