You can use baseUrl
along with paths
to fix the errors.
Paths allow us to aggregate a list directories under a predefined name and drastically reduce the length of the imports.
Here is a template on how to setup them your jsconfig.json
.
jsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@routes/*": [
"./routes/*"
],
"@models/*": [
"./models/*"
],
"@datetime/*": [
"./utils/datetime/*"
]
}
}
}
We are aggregating all the files in the models folder under the name @models. The same is the case for routes and datetime. We would be able to reference the folders using @routes, @models, and @datetime in the import statement.
So, after you've setup your jsconfig, you need to change the relative paths to absolute paths. For example:
server.js
// Before jsconfig
import adminRoutes from "../../src/routes/admin"
// After jsconfig
import adminRoutes from "@routes/admin"
I hope it helps!
Read the source for more details.