#nextjs , shadecn Module not found: Can't resolve '@/components/hooks/use-toast'

1 messages · Page 1 of 1 (latest)

magic locustBOT
#

Hi @misty glade. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The error message "Module not found: Can't resolve '@/components/hooks/use-toast'" indicates that the module resolver is unable to locate the specified file or directory. This could be due to several reasons such as incorrect file path, case sensitivity issues, or improper configuration of module path aliases.

Here are a few things you can check:

  1. File Path: Ensure that the file use-toast actually exists in the directory components/hooks/.

  2. Case Sensitivity: Some systems are case-sensitive. Make sure the casing in your import statement matches exactly with the file and directory names.

  3. Module Path Aliases: If you're using module path aliases (like @/components/hooks/use-toast), you need to configure them properly in both your Next.js and Jest (if you're using it for testing) configurations.

    In your jsconfig.json or tsconfig.json, you should have something like this:

    {
      "compilerOptions": {
        "module": "esnext",
        "moduleResolution": "bundler",
        "baseUrl": "./",
        "paths": {
          "@/components/*": ["components/*"]
        }
      }
    }
    

    And in your jest.config.js, you should have something like this:

    moduleNameMapper: {
      // ...
      '^@/components/(.*)$': '<rootDir>/components/$1',
    }
    

    This configuration tells the module resolver that @/components/ should be mapped to the components/ directory in your project root.

    If you've made changes to these configurations, make sure to restart your development server to apply the changes.

If you're still facing the issue, it might be helpful to look at the build logs or any error messages you're getting for more specific clues about what might be going wrong.

#

Sources:


Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@misty glade Kapa.ai is still learning and improving, please let me know how I did by reacting below.

misty glade
#

my current tsconfig {
"compilerOptions": {
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/": [
"./src/
"
]
},
"target": "ES2017"
},
"include": [
"next-env.d.ts",
"/*.ts",
"
/.tsx",
".next/types/**/
.ts"
],
"exclude": [
"node_modules"
]
}

#

@magic locust my current tsconfig {
"compilerOptions": {
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/": [
"./src/
"
]
},
"target": "ES2017"
},
"include": [
"next-env.d.ts",
"/*.ts",
"
/.tsx",
".next/types/**/
.ts"
],
"exclude": [
"node_modules"
]
}

magic locustBOT
#

Let me check the knowledge base for you again. Give me one moment! :books:

#

Your tsconfig.json file seems to be correctly configured for a Next.js project with TypeScript. The "paths" option is correctly set to map @/* to ./src/*, which means that you can import modules from the src directory using the @ alias.

For example, if you have a file at src/components/hooks/use-toast.ts, you can import it in another file like this:

import useToast from '@/components/hooks/use-toast'

If you're still having issues with module resolution, make sure that the file or directory you're trying to import actually exists and that the casing matches exactly. Also, remember to restart your development server whenever you make changes to tsconfig.json.

If you're using Jest for testing, you'll also need to configure the moduleNameMapper option in your jest.config.js file to handle the module path aliases. Here's how you can do it:

moduleNameMapper: {
  // ...
  '^@/(.*)$': '<rootDir>/src/$1',
}

This configuration tells Jest to map @/* to ./src/*.

Sources:


Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: