I'm getting a TypeScript warning that the module cannot be found.
I'm using TypeScript on my NextJS app and this has only occured this time while I've been using custom paths and didn't encounter this problem before.
I have this example of structure
/models
|-- index.ts
|-- User.ts
/pages
/api
/users
|-- index.ts
I'm trying to import my Userschema from models custom paths that I set but It's giving me an error
This is what my my index.ts in models looks like
export { default as User } from './User';
This is how I import it inside api/user/index.ts
import { User } from '@/models';
My tsconfig file looks like this
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"baseUrl": ".",
"paths": {
"@/components/*": [
"./components/*"
],
"@/types/*": [
"./types/*"
],
"@/utils/*": [
"./utils/*"
],
"@/middlewares/*": [
"./middlewares/*"
],
"@/models/*": [
"./models/*"
],
"@/lib/*": [
"./lib/*"
]
}
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}
Importing using custom paths other than index file like
import dbConnect from '@/utils/dbConnect' works but with index files, I still have to specify the index file name when importing to make it work. import { User } from '@/models/index'