jeroen/V8

ReferenceError with TextEncoder

fangzhou-xie opened this issue · 5 comments

Thanks for the package for bridging JS into R! I was reading JavaScript for R, Chapter 18 and was thinking if I can browserify jsdom to be used in R.

I have in.js:

global.jsdom = require('jsdom');

converted by:

browserify in.js -o jsdom.js

and then called in R:

library(V8)
jsdom <- v8()
jsdom$source("node/jsdom.js")

But then I got an error ReferenceError: TextEncoder is not defined.

My searching on Google told me that it could be Node version problem (using Node 18 first, but noticed V8 engine version 9.6.180.12 and then tried Node 16). But downgrading didn't work. I also tried to hack in whatwg-url source code, but then I got TypeError: util.TextEncoder is not a constructor error.

I am not sure why this is the case (sorry for my not being very proficient in JS), but jsdom should work fine with browserify and I think this translation should work out. And potentially I can call jsdom from R via V8.

Any help/insight will be appreciated! Thank!

Hi , have you managed to solve your issue? I have the same problem, all the function I tried calling came up with ReferenceError: Function is not defined.

Function - whichever function I tried calling.

Thanks.

Unfortunately no. I am not very familiar with JS and I only tried this because it seems straightforward. But maybe it is not as easy as it seems to be.

You are probably trying to use functions which are not part of core JavaScript. See this vignette: https://cran.r-project.org/web/packages/V8/vignettes/npm.html

I used the hints here and saved text-encoding explicitly as another dependency. Doing this will eliminate the error "ReferenceError: TextEncoder is not defined" when sourcing jsdom.js. See my blog post for details: https://nanx.me/blog/post/r-readability-parser/