FileAttachment().json is not a function

Hi all!

When I try to run the following code in node

import * as ObStdLib from '@observablehq/stdlib'
import * as Plot from '@observablehq/plot'

const { FileAttachment } = new ObStdLib.Library()

async function generateGraph() {
  return FileAttachment("athletes.json").json().then(athletes => 
    Plot.rectY(athletes, Plot.binX({y: "count"}, {x: "weight", fill: "sex"})).plot()
  )
}

const graphPromise = generateGraph()

Then I get the following exception:

  return FileAttachment("athletes.json").json().then(athletes => 
                                         ^

TypeError: FileAttachment(...).json is not a function
    at generateGraph (file:///.../argh.js:7:42)

athletes.json is in the same folder as this code.
I can’t find any examples of code like this not working online, yet I can’t seem to make it work!
Could somebody please tell me what I’m doing wrong?

Many thanks!

Hi @cactus!

FileAttachment is used in Observable notebooks to expose files to the notebook they have been attached to.

You will first have to take care of loading your data, e.g. by replacing the line

return FileAttachment("athletes.json").json().then(athletes => 

with

const response = await fetch('https://gist.githubusercontent.com/saif003/f5ac8b639a34f1c87a867d83261c0330/raw/11157fe435aa3132a6b619991101b7ae9915cbe5/athletes.json');
return response.json().then(athletes => 

(I took the data from this source).

2 Likes

Thank you so much for that solution! Really cleared things up for me.

1 Like