Daniel
Daniel

Reputation: 113

NextJS - Unexpected token < in JSON at position 0

I followed a tutorial about NextJS and have now tried to modify it a bit. I wanted to include the data.json file in the page. But I always get the error message "Unexpected token < in JSON at position 0".

I know that I should convert the JSON file into a string(?). How do I have to do that?

  let res = await fetch(`${server}/data`);
  const articles = await res.json();

  return {
    props: {
      articles,
    },
  };
}

My GIT-Repo is here: https://gitlab.com/pauernet/nextjs-test/

Upvotes: 2

Views: 5969

Answers (1)

Gh05d
Gh05d

Reputation: 8962

Your problem is that the data.json file is not in the public folder. You have to move it there from your root folder. Only files in the public folder can be accessed by domain.tld/file. Also you have to specify the full file name:

let res = await fetch(`${server}/data.json`);

Upvotes: 1

Related Questions