How can we generate vega grammar JSON from vega-lite plot?

I am able to get vega json grammar on local installation of nodejs with vega-lite and vega-lite-api installed through npm as follows:

vl = require('vega-lite') 
vla = require('vega-lite-api') 
data = require('vega-datasets') 

const df2 = data['movies.json']()

const plot = vla.markBar().data(df2).encode(
    vla.x().fieldQ('IMDB_Rating').bin(true),
    vla.y().count()
  )

vega_lite_json = plot.toObject();
vega_json = vl.compile(plot.toObject()).spec;

However I am unable to make it work in Observablehq.

I am not able to do last line: vl.compile(...).
This is what I have did:

vegalite = require("@observablehq/vega-lite@0.2")
vegalite.compile(plot.toObject()).spec;

It says: TypeError: vegalite.compile is not a function
Here is link to my observablehq notebook cell executing vegalie.compile(...).

What I am doing wrong?

Cross-posted to Stack Overflow: javascript - How can we proramatically generate vega grammar JSON from vega-lite plot? - Stack Overflow

you could try and use vegalite = vl.vegalite; or, equivalently, call:

vl.vegalite.compile(plot.toObject()).spec