nbb

2024-03-01T19:10:51.169219Z

If I have a file named backup.js with the contents const data = [ { "foo": "bar" } ], what is the best way to eval and read that data without needing to modify the file?

borkdude 2024-03-01T20:11:27.788899Z

Read the file and use Js/eval?

2024-03-01T20:13:37.460499Z

ok. That's about what I figured. Wanted to make sure there wasn't something else I was missing

2024-03-01T20:19:14.540019Z

So, it seems like doing that, I can read it if it's var data = but not const data =

borkdude 2024-03-01T20:20:45.756779Z

$ nbb -e '(js/eval "const x = 1; x")'
1

👍 1
2024-03-01T20:23:53.838619Z

I was trying to do it with js/data which only worked for var but not const or let

borkdude 2024-03-01T20:26:40.542419Z

never heard of js/data

borkdude 2024-03-01T20:26:57.233079Z

$ nbb -e '(prn js/data)'
nil

2024-03-01T20:28:07.543299Z

data was the var that was defined inside the eval-ed file. I was trying to refer to it using js/ but your way works.

borkdude 2024-03-01T20:28:41.959339Z

ah gotcha

borkdude 2024-03-01T20:31:16.387839Z

another way could be:

$ nbb -e '(def f (js/Function. "const x = 1; return x;")) (f)'
1

👍 1
borkdude 2024-03-01T20:31:26.400269Z

but you still have to add something to the source