Having trouble resolving modules after generating tsconfig.json?

I recently added a tsx component to my next.js 13 project following the documentation.

After creating the required tsconfig.json file, I encountered module not found errors when running npm run dev:

$ npm run dev        

> [email protected] dev
> next dev

ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info  - Loaded env from /Users/myuser/code/my-frontend/.env.development
info  - Loaded env from /Users/myuser/code/my-frontend/.env
We detected TypeScript in your project and created a tsconfig.json file for you.

error - ./src/pages/_app.jsx:4:0
Module not found: Can't resolve '@/styles/tailwind.css'
  2 | 
  3 | import 'focus-visible'
> 4 | import '@/styles/tailwind.css'
  5 | import { appWithTranslation } from 'next-i18next'
  6 | import { useEffect } from 'react'
  7 | import { init, push } from "@socialgouv/matomo-next";

https://nextjs.org/docs/messages/module-not-found

Running npm run build resulted in additional import issues with the @/... syntax.

The version of Node I am using is v18.14.2.

Edit: Below is the automatically generated tsconfig by Next.js without any modifications:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "incremental": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve"
  },
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

Answer №1

After generating the tsconfig.json file, I noticed that it was missing some essential lines in the compilerOptions section:

"baseUrl": ".",
 "paths": {
   "@/*": ["src/*"]
 }

To rectify this issue, I simply copied these lines from the jsconfig.json file. It's puzzling why next.js didn't include them while generating the tsconfig.

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

Tips for effectively jasmine testing with the createSpyObj function, where class properties are defined as spies

When attempting to create a mock service with set-only properties, I encountered errors indicating that the value was undefined despite following the guidance in the documentation here. I want to be able to track the values of these properties during test ...

When switching tabs, Ion-select should not reload the selected name

Whenever I switch tabs and then return to the previous tab in Ionic, the select field that was previously set becomes null, even though the page is still loading and the variable is populated. <ion-header color="primary"> <ion-navbar> &l ...

Struggling to identify the error while utilizing Jasmine's throwError function

I am relatively new to using Jasmine and have been experimenting with the toThrowError() function. However, I can't seem to get my test to pass successfully. In one of my functions, I purposely throw an error: test.service.ts test(list:{}){ if ...

What is the best way to save merged arrays collected with Apollo Client?

I'm currently utilizing Apollo Client alongside Next.js and performing a single query to fetch two different types of data: Properties and Adverts. For example: query getPropertiesAndAdverts() { search() { _id ref title ...

Typescript is issuing warnings when displaying errors for the RTK query

I am currently using React Ts and Rtk query. My goal is to display the API response error on the UI. While it's working, I am encountering a warning that prompts me to set an error type for the response errors. How can I incorporate an error type for ...

How can I turn off the draggable feature for a specific element in react-beautiful-dnd?

Currently, I am implementing a drag and drop functionality using TypeScript with react-beautiful-dnd. The goal is to allow users to move items between two containers - one containing user names and the other labeled "Unassigned". Here is a snapshot of the ...

Challenges encountered while using TypeScript to implement the CSS breakpoint mixin

I attempted to replicate the breakpoint mixin functionality described at using TypeScript, but I am having trouble correctly typing the function. import { css } from 'styled-components'; import { breakpoints } from './_variables'; exp ...

Determining the parent type in Typescript by inferring it from a nested member

Typescript has the ability to infer the type of a value based on queries made within if statements. For instance, the type of one member of an object can be deduced based on another: type ChildType = 'a' | 'b'; type Child<T extends ...

Encountering a ReferrenceError when utilizing jQuery with TypeScript

After transitioning from using JavaScript to TypeScript, I found myself reluctant to abandon jQuery. In my search for guidance on how to integrate the two, I came across several informative websites. Working with Visual Studio 2012, here is my initial atte ...

Set up a custom key combination to easily toggle between HTML and TypeScript files that share the same name

Is it possible to set up a keyboard shortcut (e.g. Ctrl + `) to toggle between mypage.html and mypage.ts files? In my project, I have one HTML file and one TypeScript (TS) file with the same names. Ideally, I'd like to create a hotkey similar to F7 fo ...

Incorporate a New Feature into my NPM Package

I've been searching high and low for an answer to this, but I'm still stuck. I'm working on a module in Angular 2 with ng-module, and everything is functioning properly. However, I'm struggling to assign a property to another property w ...

Enhancing code completion with IntelliSense for customized styled components' themes

When using a theme in styled components, I am attempting to enable IntelliSense. In my code snippet below (index.tsx), I utilize ThemeProvider: import React from 'react'; import ReactDOM from 'react-dom/client'; import { ThemeProvider } ...

During the transpiling process, the metadata of an Angular component may become lost

Encountering another error: Uncaught Error: Unexpected value 'UserDialogComponent' declared by the module 'AppModule'. Please add a @Pipe/@Directive/@Component annotation. Current behavior Summary: When incorporating an external libra ...

A Guide to Filtering MongoDB Data Using Array Values

I am trying to extract specific data from a document in my collection that contains values stored in an array. { "name": "ABC", "details": [ {"color": "red", "price": 20000}, {" ...

How does the NEXT_DATA script in the Nex.tjs web application make my confidential variables accessible to the client side?

I recently developed and launched a next.js web application where I meticulously selected my environment variables and stored them in the next.config.js file. To protect these variables, I added them to the gitignore file. However, upon inspecting the br ...

Step-by-step guide on importing SVG images in next.js with the file-loader

I've implemented the following loading configuration: config.module.rules.unshift({ test: /\.svg$/, use: { loader: 'file-loader', options: { esModule: false, name: '[name]-[hash].[ext]', outputPat ...

Error encountered during Typescript compilation: Type 'void' cannot be assigned to type 'Item[]'

Below are my typescript functions. When I edit in vscode, the second function does not show any error message. However, upon compilation, an error is displayed for the second function: error TS2322: Type 'Promise<void>' is not assignable t ...

Steps to specify a prefix for declaring a string data type:

Can we define a string type that must start with a specific prefix? For instance, like this: type Path = 'site/' + string; let path1: Path = 'site/index'; // Valid let path2: Path = 'app/index'; // Invalid ...

Troubleshooting Azure typescript function: Entry point for function cannot be determined

project structure: <root-directory> ├── README.md ├── dist ├── bin ├── dependencies ├── host.json ├── local.settings.json ├── node_modules ├── package-lock.json ├── package.json ├── sealwork ...

What is the best way to skip the additional step of "Sign In With Cognito" in next-auth and go straight to the sign-in form?

When working on my Next.js project and utilizing Cognito for sign-in through the next-auth library, I encounter a initial step where I have to click a button labeled "Sign in with Cognito" before being directed to the actual sign-in form. Is there a way to ...