skip to Main Content

I’m learning nextJS 15 and I’m making an app using the pokeAPI but when I run npm run build eslint gives me the following message:

Error: ‘error’ is defined but never used. @typescript-eslint/no-unused-vars

The following code is the one producing the error:

const getPokemon = async (id: string) => {
    try {
        const pokemon = await fetch(`https://pokeapi.co/api/v2/pokemon/${id}`, {
            cache: "force-cache"
        }).then(res => res.json());
        return pokemon;
    } catch (error) {
        notFound();
    }
};

Is there a way to ignore that unused variable specifically?

Here’s the .eslintrc.json:

{
    "extends": ["next/core-web-vitals", "next/typescript"]
}

I tried the no-unused-vars rule but it didn’t work

2

Answers


  1. There are two ways you can go about fixing this

    1. Disable the eslint rule for just that file alone: You can do this by pasting this at the top of the file /* eslint-disable @typescript-eslint/no-unused-vars */
    2. You can set a disable/turnoff the rule in all the files in your project by adding a rule object to your eslintrc.json file

    eslintrc.json

    {
        "extends": ["next/core-web-vitals", "next/typescript"],
        "rules":{
         "@typescript-eslint/no-unused-vars": "off"
        }
    }
    
    Login or Signup to reply.
  2. You can simply write the try catch without binding the error to a variable:

    try {
      /* ... */
    } catch {
      notFound();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search