3

In Docusaurus V2 how to link to a JSON file in the static folder?

I tried the following in a markdown file:

An exemple, is the following [JSON dataset](../../static/data/solar-radiation.json).

But Docusaurus then produce the following error:

./static/data/solar-radiation.json (./node_modules/file-loader/dist/cjs.js?name=assets/files/[name]-[hash].[ext]!./static/data/solar-radiation.json)
Module parse failed: Unexpected token e in JSON at position 0 while parsing near 'export default __web...'
File was processed with these loaders:
 * ./node_modules/file-loader/dist/cjs.js
You may need an additional loader to handle the result of these loaders.
SyntaxError: Unexpected token e in JSON at position 0 while parsing near 'export default __web...'
    at JSON.parse (<anonymous>)

My file is a valid JSON. For some reason, instead of displaying a static file Docusaurus seems to try to parse it...

Lucas S.
  • 312
  • 3
  • 15

2 Answers2

5

I saw the same problem. My temporary solution is:

  • Change file name from .md to .mdx
  • Add this link to your json file
<a target="_blank" href="/json/file.json" download="file.json">Download</a>
Cananau Cristian
  • 436
  • 2
  • 6
  • 30
1

I've also had the same problem!

According to the Issue #3561 on the Docusaurus GitHub, word from one of the developers is that the current best practise is to use the pathname:// prefix in front of your URL. To use your example:

An exemple, is the following
[JSON dataset](pathname://../../static/data/solar-radiation.json).

Apparently the reason why this happens is because there is a conflict between the babel-loader (used to load code) and the file-loader (used to load static assets). The pathname:// prefix skips both of these loaders, and just creates a link instead.

Alois Klink
  • 646
  • 8
  • 9