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 => 


const response = await fetch('');
return response.json().then(athletes => 

(I took the data from this source).


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

1 Like