Note! I went through many answers before asking the question but did not find the answer I want. Please, do not mark it as a duplicate.
In es modules we can set baseUrl in jsconfig.json:
{
"compilerOptions": {
"baseUrl": "/base"
}
}
or
{
"compilerOptions": {
"baseUrl": "."
}
}
Then we can import any files from that base directory throughout the project:
import Grand from 'base/child/grands'
I want to achieve the same thing in commonjs syntax (which uses require() ). I want to be able to import files(modules) like this:
const Grand = require('base/child/grands')
not this way:
const Grand = require('./base/child/grands')
How can I achieve this?
P.S. I am working on a Next.js project which uses 'es modules' for frontend and 'commonjs' for backend.