1

Today I decided to compete in an online programming contest using JavaScript for the first time but It got me in trouble! My local version of Nodejs was v10.16.1 but the online judge used V8 JavaScript engine.

Until today I thought Nodejs uses V8 as JavaScript engine; however unfortunately I can't use readline and print built-in functions of d8 in Nodejs today.

So Does Nodejs support V8 by default?

  • If no, how can I install d8 alongside Nodejs and how can I use it?
  • If yes, how can I enable it?

Any response would be appreciated...

Edit: As far as I realized, that online judge isn't embedding V8; It just uses d8 as it's environment which is a shell (interface) for V8 and readline and print are the built-in functions of the d8 (not V8).

Edit: This question is related to using JavaScript in online contests. Also this one is helpful for how to use d8.

mrzrm
  • 926
  • 7
  • 19
  • Node.js uses V8, but `print` and `readline` are not built-in functions in V8. – Teemu Mar 06 '20 at 16:06
  • You need to add `const readline = require('readline');` at the top of your script, to be able to use it. Maybe the contest platform does that for you – blex Mar 06 '20 at 16:07
  • @Teemu https://v8.dev/docs/d8 – mrzrm Mar 06 '20 at 16:08
  • @blex I know I can require `readline` but in V8 we don't need it! Check it here https://v8.dev/docs/d8 – mrzrm Mar 06 '20 at 16:10
  • 2
    `d8 !== V8` ... – Teemu Mar 06 '20 at 16:10
  • `V8 !== NodeJS` NodeJS uses V8. Read the NodeJS docs: https://nodejs.org/api/readline.html#readline_readline – blex Mar 06 '20 at 16:10
  • Nodejs is a server environment, which uses Google's V8 as its JavaScript engine. – Teemu Mar 06 '20 at 16:14
  • I'm confusing a bit. What are d8, V8, Nodejs v8? – mrzrm Mar 06 '20 at 16:14
  • 1
    @mrzrm V8 - JS engine, d8 - JS shell for V8, Node.js v8 - version of Node.js – LEQADA Mar 06 '20 at 16:17
  • As the article you linked says: "_d8 is V8’s own developer shell. [It is] useful for running some JavaScript locally or debugging changes you have made to V8_". – Teemu Mar 06 '20 at 16:19
  • @LEQADA So how can I use d8 locally? Is there any pre-built version to install? – mrzrm Mar 06 '20 at 16:20
  • @Teemu I already know but some comments made me confused! – mrzrm Mar 06 '20 at 16:22
  • 1
    D8 is basically a sample program by the V8 developers to show how to use V8 as a library. Google Chrome for example does not include D8 code even though it uses V8 so Google Chrome does not have `print` and `readline`. Node.js did the same thing, they wrote a competitor/alternative to D8 and not include any D8 code in Node instead choosing to create the function `console.log` and the `readline` and `fs` modules – slebetman Mar 06 '20 at 17:20

1 Answers1

3

V8 is a Javascript engine. It does not have a user interface of it's own so it can't run Javascript all by itself. It's for developers. A developer links V8 into their program in order to be able to run Javascript from their program.

The Chrome browser uses V8.

Nodejs uses V8.

D8 (a programming shell) uses V8.

So, if you want to run Javascript with V8, you must run one of these programs that has V8 built into it.

So Does Nodejs support V8 by default?

Yes, V8 is built into Nodejs.

If no, how can I install V8 alongside Nodejs and how can I use it? (I prefer pre-built and binary version)

It's already built in.

If yes, how can I enable it?

It is enabled by default in the Nodejs environment.

Until today I thought Nodejs uses V8 as JavaScript engine; however unfortunately I can't use readline and print built-in functions of V8 in Nodejs today.

You would have to show your specific code and problem using readline in node.js. readline is built-into node.js and is not part of V8. The documentation for something like readline shows examples quite clearly of how to use it. You must first load the readline module and then use methods from that module.

Today I decided to compete in an online programming contest using JavaScript for the first time but It got me in trouble! My local version of Nodejs was v10.16.1 but the online judge used V8 JavaScript engine.

It's unlikely the online contest was using V8 directly. They were likely using a programming environment that itself has V8 linked in. You would need to be more specific about exactly what programming environment the programming contest is using. Is it using a specific version of Nodejs? Or a programming shell like D8? Or a browser?

mrzrm
  • 926
  • 7
  • 19
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • "*A developer links V8 into their program in order to be able to run Javascript from their program.*" How do they do it? – mrzrm Mar 06 '20 at 16:37
  • @mrzrm - You start [here](https://v8.dev/docs). Then, you can read the doc on [embedding V8](https://v8.dev/docs/embed) into your program. V8 has a C++ programming interface so that's what you'll be using to interface with it directly and to embed it in your program. – jfriend00 Mar 06 '20 at 16:39
  • *You would have to show your specific code and problem using `readline` in node.js. readline is built-into node.js and is not part of V8.* I saw [here](https://v8.dev/docs/d8) d8 doesn't need to require it. So I thought it's built-in V8 – mrzrm Mar 06 '20 at 16:48
  • *Is it using a specific version of Nodejs? Or a programming shell like D8? Or a browser?* They uses D8 as their environment. – mrzrm Mar 06 '20 at 16:55
  • Thanks for your answer. Is there a pre-built version of D8 which I can install it? or I should build it by own? – mrzrm Mar 06 '20 at 16:57
  • 1
    @mrzrm - Everything I've seen says you build D8 yourself. FYI, your question itself does not mention D8 at all. I know you mentioned that in your comments, but if someone was just reading your question, they would be confused since you don't run V8 directly by itself. – jfriend00 Mar 06 '20 at 17:01