0

I understand node.js allows us to write javascript and run it server-side, instead of in the client's browser.

I have a question, though, is it fully javascript? Could I, for example, just get a javascript game I wrote, copy paste the code in the appropriate place in node.js, and it would run perfectly? Or would I need to make changes to my javascript code (e.g. syntax, libraries (suppose the code uses Jquery and box2dWeb))?

1
  • It is a different language that runs on Google Chrome's V8 JavaScript Compiler, so all the javaScript methods are inbuilt into it as far I understood, it does not contain the Browser element into it. If you look closely into the code of Node.js you will be able to understand more deeply how Node.js is making use of JavaScript Commented Oct 20, 2015 at 22:56

2 Answers 2

3

node.js is an implementation of ECMAScript plus a number of additional libraries that let you do things like access the file system, do native TCP networking, etc...

The Javascript in a browser is an implementation of ECMAScript plus a number of host objects in the browser that let you manipulate things in the browser (such as the displayed document).

Only pure Javascript that does not use any of the browser host objects (e.g. does not access the DOM and does not use any browser-specific APIs) could run in both places.

Could I, for example, just get a javascript game I wrote, copy paste the code in the appropriate place in node.js, and it would run perfectly?

No. Assuming your game has a user interface, there is no browser-like user interface in node.js.

Or would I need to make changes to my javascript code (e.g. syntax, libraries (suppose the code uses Jquery and box2dWeb))?

It depends upon what non pure ECMAScript things your current code uses. All of those non pure ECMAScript things (like DOM objects in the browser) would have to be modified and replaced with something else.


What some people do not realize is that ECMAScript (which is what is in common between browser Javascript and node.js) is just the language itself and does not include things like the window object, the document object, methods to create or find DOM elements, etc... Those are "host" methods or objects which are specific to the browser and not specific to ECMAScript.

Sign up to request clarification or add additional context in comments.

Comments

1

it's just the language. It's the complete language. But javascript in a browser marries javascript with the DOM, so you can script it. Node doesn't include the browser. So your game, if it makes use of the DOM, won't work.

There are bindings for Node to do all kinds of things....

you can use something like Cordova and put your game on Android / Iphones

you can use something like electron and you get your browser back and targeted to being an application on linux/windows/osx

But you will have to change your code to get it to work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.