Trouble with 'import type' declaration causing build issues in a Next.js project

Having trouble importing the Metadata type from the next module.

The code snippet below is directly from the Next.js documentation.

THE ISSUE

client.js:1 ./app/layout.tsx:3:12
Syntax error: Unexpected token, expected "from"

1 | import React from 'react';
2 | import './globals.css';

> 3 | import type { Metadata } from 'next';
> |             ^
> 4 | import { Inter } from 'next/font/google';
> 5 |
> 6 | const inter = Inter({ subsets: ['latin'] });

app/layout.tsx

import React from 'react';
import './globals.css';
import type { Metadata } from 'next';
import { Inter } from 'next/font/google';

const inter = Inter({ subsets: ['latin'] });

export const metadata: Metadata = {
  title: 'Play Chopsticks',
  description:
    'Some call it finger chess, some call it magic fingers. Test your skills and become the number one chopsticks master',
};

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang='en'>
      <body className={inter.className}>{children}</body>
    </html>
  );
}

// tsconfig.json

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

Not sure if my tsconfig.json is set up correctly. It excludes the 'node_modules' directory, but the 'next' module is located within node_modules.

Answer №1

Success:

After some investigation, I discovered an interesting thing in my project directory - a hidden .babelrc file. It seems that having this .babelrc file automatically disables the 'Nextjs compiler'.

The 'Nextjs Compiler' appears to be necessary for handling TypeScript imports from the 'next' module.

So, with a bold move, I decided to delete the mysterious .babelrc file and voila... the code compiled successfully! #insert Borat quote here#

While the exact mechanism behind this solution remains unclear to me, it did the trick... If anyone has more insights into this, please share your knowledge!

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

Attempting to create a Next.js 13 application, but struggling with using the client-side functionality

Exploring Next.js for the first time, I embarked on creating a simple application. Everything was going smoothly until I attempted to include a "use client" tag at the beginning of a component to utilize certain hooks. This resulted in the app breaking and ...

How to Add a Rule to an Existing Application Load Balancer Listener using AWS CDK

When I inherited a project, I discovered an application load balancer with a HTTPS Listener that was set up before I began using CDK. This listener currently has 13 rules in place that route requests based on hostname to different fargate instances, with ...

Unlocking 'this' Within a Promise

I seem to have an issue with the 'this' reference in the typescript function provided. It is not correctly resolving to the instance of EmailValidator as expected. How can I fix this so that it points to the correct instance of EmailVaildator, al ...

Tips for transferring the id from a delete button to a delete button in a popup dialog box

In my frontend application, there is a table where each row corresponds to an item. For every row, there is a "Remove" button that triggers a warning popup upon being clicked. The intention is to pass the item's ID in this popup so that if the user co ...

Contending with a React challenge: Dilemma of variable

I'm currently working on a React component that performs a request to retrieve a list of books upon mounting. Additionally, it subscribes to a hub that sends Server Sent Events to notify React of any updates to the dataset. However, I've encount ...

What's causing the subscription feature to malfunction in a fresh browser tab?

I am facing an issue with camera entries on an angular website. Whenever I click on an entry, a new window opens to display the camera livestream. However, I am having trouble with the subscribe functionality. Important note: Once the window is open, subs ...

Tips for changing the color of the placeholder in Material UI OutlinedInput

I've been attempting to change the placeholder color of the MaterialUI input field but haven't been successful so far. Here's what I've tried: const useStyles = makeStyles((theme) => ({ emailField: { width: 265, ...

NextJS - Error: Invalid JSON format, starting with a "<" symbol at position 0

After following a tutorial on NextJS, I attempted to make some modifications. My goal was to include the data.json file on the page. However, I kept encountering the error message "Unexpected token < in JSON at position 0." I understand that I need to con ...

React useEffect appended script not activating

I'm having trouble loading a script that should generate an iframe and display a weather widget upon trigger. I am able to append the script correctly, but it doesn't seem to be called and does not create the iframe. Even when I directly add the ...

Creating an Angular table using reactive forms: a step-by-step guide

After reviewing the HTML snippet provided below, it is evident that there is a table with looping through mat cell using *matCellDef="let model". Inside each cell, there are input fields which are reactive forms. Each row or cell needs to have it ...

"Enhancing the functionality of @angular/fire by transitioning from version 6.x to 7.x

I need to update my app dependencies and code from @angular/fire 6.x to 7.1.0-rc4 in order to access a feature that is not available in the 7.0.x version. I made the necessary changes in my package.json as follows: "@angular/fire": "~7.1.0-r ...

The onTableChange function in Mui-Datatable does not seem to be functioning properly when attempting to

Utilizing mui-datatable, I came across an official example on codesandbox where setState can be used on the tableState. You can check out the example here: https://codesandbox.io/s/trusting-jackson-k6t7ot?file=/examples/on-table-init/index.js handleTableIn ...

Error: Attempting to access 'push' property on an undefined object

Encountered an Error After implementing the useNavigate function, I successfully resolved the issue. let params = useParams(); let navigate = useNavigate(); const dispatch = useDispatch(); const productId = params.id; const [qty, setQty] = useStat ...

Utilizing the power of React Material UI in conjunction with React Router

I'm facing an issue in my app where I have a sidebar and appbar. When I try to open a page using Link(react router), it opens within the body of the sidebar and appbar instead of opening up as a normal HTML page without them. How can I make this work ...

How can I employ CSS files within a Node module that is compatible with Next?

I recently made the switch from Gatsby to Next and I'm still learning the ropes. When working with Gatsby, I had a Node module that served as my UI library across different projects. This module utilized a CSS module file (style.module.css) that coul ...

Angular2 with Typescript is experiencing issues with the implementation of operations in JavaScript

I'm struggling to implement a D3 graph example in my Angular 2 TypeScript application. The issue arises with the lines marked with "----> Error". I have no clue on how to tackle this problem. Can anyone offer some assistance? d3.transition().duratio ...

Guide to successfully setting up a Json server and ReactJS to run simultaneously on the same port

Currently, I am facing an issue while attempting to run my React application with a JSON server as the backend API. The problem arises when trying to run them on different ports due to a CORS error. I am currently seeking advice and looking for a solutio ...

Issue: An object with keys {} is not suitable as a React child, causing an error

I am new to TypeScript and seeking help from the community. Currently, I am working on a to-do list project where I am using React and TypeScript together for the first time. However, I encountered an error that I cannot decipher. Any assistance would be g ...

Can TypeScript modules be designed to function in this way?

Seeking to create a versatile function / module / class that can be called in various ways: const myvar = MyModule('a parameter').methodA().methodB().methodC(); //and also this option should work const myvar = MyModule('a parameter') ...

Inject SCSS variables into Typescript in Vue 2 Using Vue-cli 5

When working on my Vue 2 project (created using the Vue-cli v4), I successfully imported variables from my SCSS file into my typescript (.vue files) without any issues. I had the :export { ... } in my SCSS file _variables.scss, along with shims.scss.d.ts ...