Error: Unable to locate module '@/components/Header' in Next.js project

I'm currently facing an issue while working on my Next.js project. The problem arises when I attempt to import a component into my 'page.js' file. In the 'src' folder, I have a subdirectory named 'components' which contains a file called 'Header.js'. Within this file, there is a function component called 'Header'. My import statement in 'page.js' looks like this:

import Header from "@/components/Header";

However, I am encountering the following error: Module not found: Can't resolve '@/components/Header'

I have double-checked to ensure that the file exists in the correct location and that the path is accurate. For reference, here is the structure of my project:

src/
├── components/
│   └── Header.js
└── pages/
    └── page.js

Header.js

export default function Header() {
  return(
    <header className="bg-white border-b flex justify-between p-4">
        <div className='flex gap-6'>
        <Link href={'/'}>Linkify</Link>
        <nav className='flex items-center gap-4 text-slate-500 text-sm'>
          <Link href={'/about'}>About</Link>
          <Link href={'/pricing'}>Pricing</Link>
          <Link href={'/contact'}>Contact</Link>
        </nav>
        </div>
        <nav className='flex items-center gap-4 text-sm text-slate-500'>
          <Link href={'/login'}>Sign In</Link>
          <Link href={'/register'}>Create Account</Link>
        </nav>
      </header>
  )
}

page.js

import Header from "@/components/Header";


export default function Home() {
  return (
    <main>
      <Header />
      <section>

      </section>
    </main>
    
  )
}

If you have any suggestions or solutions, your help would be greatly appreciated. Thank you!

Answer №1

If you want to utilize this feature, make sure to set up the alias path for @ within the specific file called tsconfig.json. Consider including the following configuration:

{   compilerOptions: {
      ...
      "paths": {
         "@/*": [
           "./*" // -> modify the path to your desired location
         ]
      },    
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transforming the date from JavaScript to the Swift JSON timeIntervalSinceReferenceDate structure

If I have a JavaScript date, what is the best way to convert it to match the format used in Swift JSON encoding? For example, how can I obtain a value of 620102769.132999 for a date like 2020-08-26 02:46:09? ...

No action is triggered after submitting AJAX data in a WordPress environment

I'm currently working on developing a WordPress plugin that requires querying the database. However, I am facing challenges in getting it to function properly. Here is the code snippet that I have attempted: jQuery('#demo_ajax').submit(func ...

What is the best way to delete a nested child object using a specific identification number?

Here is the current json structure: $scope.dataList = [{ CompanyName: null, Location: null, Client: [{ ClientId: 0, ClientName: null, Projects:{ Id: 0, Name: null, } }] }]; I'm attempting to remo ...

The module cannot be required as a function for calculating the area of a square

None of the functions I created seem to be working properly. Take a look at this example function: function calculateArea(side) { var area = side * side; return area; } When I attempt to use the module using require, like so: var formulas = require( ...

Transferring an organized array to processing

My goal is to transfer an array of integers from a php file called load.php to a JS script, which will then forward it to a Processing file written in JavaScript. In load.php, I define the array and send it using JSON (the array contains a minimum of 40 i ...

Utilizing Angular 11's HostListener to capture click events and retrieve the value of an object

Using the HostListener directive, I am listening for the click event on elements of the DOM. @HostListener('click', ['$event.target']) onClick(e) { console.log("event", e) } Upon clicking a button tag, the "e" object contains the fol ...

Step-by-step guide to enabling native Bootstrap 2.3.2 JavaScript elements within Liferay

In my web development project, I am working with Liferay 6.2 and utilizing Alloy UI's version of Bootstrap in the html pages by adding these lines: <link href="http://cdn.alloyui.com/2.5.0/aui-css/css/bootstrap.min.css" rel="stylesheet" /> < ...

When JavaScript is turned off, combining useSWR with getServerSideProps and GraphQL results in a blank page

Currently, I am in the process of creating a page using Next.js with the pages/api feature that connects to graphql using the graphql-yoga dependency and fetching data from mongoose via http://localhost:3000/api/graphql. My objective is to develop a serve ...

Navigating within a class-based component using Next.js: A brief guide

Is it possible to create a redirect from within an onSubmit call while maintaining CampaignNew as a class-based component? I have provided the code snippet below, and I am looking for guidance on achieving this. import React, { Component } from "rea ...

An issue with the image filter function in JavaScript

I am currently working on a simple application that applies image filters to images. Below is the code I have written for this purpose. class ImageUtil { static getCanvas(width, height) { var canvas = document.querySelector("canvas"); canvas.widt ...

Adjust CSS classes as user scrolls using skrollr

I am currently facing an issue with the prinzhorn/skrollr plugin when trying to use removeClass/addClass on scroll function. I have attempted to find a solution but unfortunately, nothing has worked for me. <li class="tab col s3"><a data-800="@cl ...

Tips for Using AJAX and JavaScript to Save an XML File

My current task involves attempting to insert an element into an XML file. Upon inspecting the program with a debugger, I noticed that the element is successfully added to the XML file. However, when I stop the program from running, the changes are not sav ...

Issue with Vue.js: document.querySelector is returning a null value even though the element clearly exists

I'm currently working on implementing a responsive navbar following Kevin Powell's tutorial, but I've run into an issue. For some reason, when I try to select the element with the class 'primary-navigation' using 'const primar ...

A collection of collections

Alright, listen up. I've got a JSON file with an array inside another array. Here's a snippet of the JSON file: { "keys": [ { "game": "Counter-Strike: Global Offensive", "price": "5", "listofkeys" ...

Having trouble loading CSS in an express view with a router

I am encountering an issue where I am unable to load my CSS into a view that is being rendered from a Router. The CSS loads perfectly fine for a view rendered from app.js at the root of the project directory. Below is my current directory structure, node_ ...

Every div must have at least one checkbox checked

Coding in HTML <div class="response"> <input type="checkbox" /> <input type="checkbox" /> <input type="checkbox" /> <input type="checkbox" /> </div> <div class="response"> <input type="check ...

Is there a way for me to maintain a consistent layout across all pages while also changing the content component based on the URL route in Next.js?

I'm currently working with Typescript and Next.js My goal is to implement a unified <Layout> for all pages on my website. The layout comprises components such as <Header>, <Footer>, <Sidenav>, and <Content>. Here is the ...

Transmitting form information to a nested page within a div container

This is my fourth attempt to seek an answer to this question, as I've faced downvotes in previous attempts. So here we go again. I am trying to send data from a hidden input within a form using ajax. The value of the hidden input is generated by a php ...

Having trouble accessing req.user on my Node.js server using Auth0 and Angular

Currently, I am utilizing auth0 for my admin panel's login system and it is functioning smoothly. However, I have encountered an issue in node where 'req.user' is returning as undefined for some unknown reason. This setup is fairly basic; I ...

Unable to load JSON information into an ExtJS grid

I have exhausted all the solutions I found (there are many on Stackoverflow) in an attempt to fix my extjs store issue. Despite ensuring that the grid is displayed correctly, the data still refuses to show up. Feeling perplexed, I am sharing the "store" co ...