DEV Community

Hash
Hash

Posted on • Updated on

An introduction to Node.js

How do you run your JS code?

With a browser console!
And do you know how it is run ?
Probably if you are using chrome you may be heard of V8 engine.

Yes there is an engine that runs your code, but there are different engines, for instance:

SpiderMonkey is used in Mozilla Firefox
JavaScript Core is used in Apple Safari
Chakra is used in Internet Explorer
V8 in used in Edge, Chrome, Brave, ...

Then Developers were going to find a way to run JS without browser? How we can do that?

Yes, you guessed right With Node.js

Google introduced Chrome browser with v8 engine in 2008.
one year later, Node.js was introduced, running JS code outside the browsers.

Now you can run your JS code on client and backend side, as a Full stack.

But Node.js is not a programming language, it's not a framework, but a Runtime. It's written by C++.

*It's an official definition: *
Node.js is an open-source, cross-platform, non-blocking, asynchronous, event driven, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser.

What is open-source:

Node.js is released under a license in which the copyright holder grants users the rights to use, study, change, and distribute the software and its source code to anyone and for any purpose.

What is cross-platform :

Node.js allows us to build cross-platform desktop apps on platforms such as NW.js or Electron. This allows you to reuse some code from the web app to the desktop version for Windows, macOS, and Linux

What is asynchronous and Non-Blocking ?

Synchronous means run code line by line.
In programming, synchronous operations block instructions until the task is completed, while asynchronous operations can execute without blocking other operations
For example stringify is blocking code.

JSON.stringigy({ food:'Ghorme Sabzi' })
Enter fullscreen mode Exit fullscreen mode

and setTimeout is non-blocking code.

setTimeout(myFunction, 1000);
Enter fullscreen mode Exit fullscreen mode

What is runtime, let's demystify that:

JavaScript runtime provides an environment for us to run JavaScript code. That gives us a little bit more than the V8 engine.
JavaScript runtime runs code and whenever it needs some I/O it will communicate with libuv.

libuv library is a multi-platform C library that provides support for async I/O based on event loops.

Is a web browser a JavaScript Runtime?

Yes, A browser contains a JavaScript engine. The engine implements a JavaScript runtime, which includes the call stack, heap and event loop.

In browser, we have some features for instance window object that allows us to use some functionality regarding browser but if you run it on Node.js runtime you will get an error, instead on Node.js runtime There is another object named global that works but not in browser. So V8 Engine is customized and integrated with some other features in each runtime (browser, Node.js)

The NODE.JS SYSTEM

Node.js System

Node.js includes V8, APIs, Bindings and libuv

Node.js APIs

  • fs
  • http
  • path
  • crypto -...

In order to write and run code in Node.js you can download Node.js or run your code online on replit

after downloading and installing Node.js you can open a terminal and type node, you should see something like this:

hash@HMacBook~ % node
Welcome to Node.js v14.17.0.
Type ".help" for more information.
> "ha"+"sh"
'hash'
Enter fullscreen mode Exit fullscreen mode

Now you can write your js to be run, this environment called repl that stands for read, eval, print and loop.

Who uses Node.js

Companies that use Node.js include the following:

LinkedIn, Netflix, Uber, Trello, PayPal, NASA,eBay, Medium,
Walmart, Mozilla

Please share your Ideas about Node.js in comments.

Thanks for reading

Top comments (0)