Skip to content

Introduction to NodeJS: How and Why Use JavaScript’s Awesome Runtime

Learn to learn code faster, using techniques honed by years of study, neatly packaged into one book: How to Learn Code

Node is a runtime for JavaScript. Think like Java JRE, .NET Runtime or the Python runtime. These include the compiler, standard libraries and middle ware between the language and the OS.

Not all languages include a complete runtime. For example-once you put together a C++ runtime it works for that specific system. C and C++ have various compilers and language implementations which work only for a specific OS or platform.

Node, Python and Java JRE runtimes are cross-platform and once installed work on all systems. They are a central software that converts programming language to machine code in execution. 

Developers love Node’s for its many features of the runtime. We programmers love Node for its cross-platform ability, package installation, ease of installation and ecosystem.

Javascript code

When we think Node think a way to run JavaScript in machine code.

Google V8 Engine history

In a distant past 2009-the original Node developer Ryan Dahl wanted a way to run JavaScript as machine code outside the browser. He was searching for a way to put together a runtime.

He needed a compiler, executor, libraries, bindings to the OS and many features. The most important and famous part of this development is the compiler and executor. To this day we consider it so important it is the most famous part of Node.

Ryan decided to include a existing compiler and executor for JavaScript-used in internet browsers. The engine converts JavaScript language to machine code then runs it. 

At the time there were many engines running and compiling JavaScript in the browser. SpiderMonkey, Rhino, Nitro, KJS and Google’s V8 engine were used across the various browsers of the time. Nitro was used by Safari, SpiderMonkey by Fire Fox, V8 by Chrome, etc. Each was implemented differently and most in Java or C++. 

https://v8.dev/

Among the engines existing in 2009 he decided on Google’s V8 engine. V8 had been used across devices in Chrome for the past year. He chose the engine based on its superior performance in C++.

With the engine he put together a cross-platform runtime and since 2009 Node has been built for JavaScript. Node has become the standard platform for developing Javascript.

From Ryan’s project we have package installation, cross-platform coding and single threaded applications with asynchronous I/O. 

Node use cases

Node’ asynchronous and single-threaded nature work well for certain tasks-it is good for web servers, I/O applications, APIs, general programming, page applications and video/sound streaming. 

It is not good for multi-threaded applications, intense CPU tasks, Desktop applications and heavy server applications like multi-core software.

Typescript

A major use case of Node is its library for Typescript. Typescript is a language wrapper for JavaScript. It is compiled down to JavaScript (in Node) then run by Node again.

https://www.typescriptlang.org/

Nowadays programmers use this language wrapper to make JavaScript more readable with typing.

JavaScript is known for being easy to write but hard to read. Why? Because it does not enforce writing the type. It is typeless.

Look at this:

function sum(a, b) {
    return a + b;
}

let result = sum(5, 10);
console.log(result);  // Output: 15

let anotherResult = sum("5", "10");
console.log(anotherResult);  // Output: "510"

Notice two different types are used in the same function and produce a different result. 

Though this is great for writing a lot of code, very fast. The code becomes more illegible as the code base grows. This is why Typescript is so popular.

Let’s look at the same example in TypeScript:

function sum(a: number, b: number): number {
    return a + b;
}

let result = sum(5, 10);
console.log(result);  // Output: 15

// Error here
let anotherResult = sum("5", "10"); //Type requires number
console.log(anotherResult);

In the function header we require numbers. From the called sum function we cannot use strings. Though our code size gets bigger we gain a substantial difference to legibility.

For Typescript the increased code size is worth the legibility.

Not only can Node run Typescript-it also loses no performance in its runtime.

JavaScript language server

Other companies and developers create language servers for various languages. The Node runtime does not include a language server.

A language server is a backend web server which allows for autocomplete, syntax correct, find all, replacing, debugging and many more things done in the editor.

https://code.visualstudio.com/

IDE developers include it in their editor like Jetbrains or VS Code. JavaScript/Typescript developers use the IDE, runtime, language server and many other tools to develop JavaScript projects.

How to build projects 

There are some notable features coders use in Node. They are NPM, Yarn, packages and the build folder. If we understand these components then we can easily understand Node.

NPM 

Node Package Manager allows you to install JavaScript libraries easily with simple commands like these:

npm init // Creates an NPM Node project
npm install express

NPM is an API, requiring internet to install libraries from an online repository called the NPM registry

The official website is npmjs.com…

https://www.npmjs.com/search?q=express

You can pull any package on that website from NPM. JavaScript developers can publish libraries on NPM as public or private. Entire NPM library repositories can be made private or public and us consumers of the library can include them in NPM given the right credentials.

To include private repositories we would use a command like so…

npm config set registry https://username:password@example.com/npm

We set a registry using NPM but replace the username and password. If we wanted to publish a package in a registry we would use…

npm publish
## or
npm publish --registry=https://example.com/mycompany-registry

You can use one registry at a time.

Local Vs Global

With NPM we can install libraries either in our local project or across all projects. When we include it only in one project it is called local and when we include it with Node itself it is called global.

Whenever we use that specific Node software on our computer we use all the libraries globally and on our local project.

Typically Node places all its global libraries in one folder on your machine.

## Linux/Mac
/usr/local/lib/node_modules

## Windows
%AppData%\npm\node_modules

The name Node uses is always node_modules. When using NPM there will be a local node_modules and a public node modules.

Take a look…

My local project

Notice my local project “my-app”. All libraries I install will be placed here. If I install a library globally then it will go to Node’s node_modules.

When I finally run the application both the local and global libraries are used. This means any global library in all of my local projects. 

We can run an entire node project with only the global libraries if we wanted.

Here is how you can install global libraries…

npm install -g express

With this command you install express in the global node_modules and make the library not needed in the local node_modules.

package.json

With package.json NPM tracks installed libraries. Here is a tiny example of a package.json.

{
  "name": "my-express-app",
  "version": "1.0.0",
  "description": "A simple Express.js application",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "^4.17.1"
  }
}

We have named our application, declared a version of our application along with a description of the application. In the package.json we noted the main function.

The scripts are direct mappings of a command to a script. This allows for automation, take a look….

npm run <script>
## Meaning
npm run start
## Meaning
node index.js

We run node index.js using the start command in NPM. Package.json reverses the node app down to the main function and voila our app runs…

Lastly we have dependencies. Here we show the library name and its version. We can change the version or add libraries here then run npm install…

npm install

npm install searches the package.json and re-installs the local node_modules depending on package.json.

In between package.json and node_modules is npm install. In the end the application uses the node_modules to run in Node.

package-lock.json

We include package-lock.json to specify the exact versions of libraries to that project. Often when we change versions of our libraries we need to delete package-lock.json due to how it specifies the project.

NPM creates a new package-lock.json on npm install.

Yarn

Yarn is an improved version of NPM using the same command structure and registry. It is faster and includes more features allowing it to outperform NPM.

Commands include…

yarn init
yarn add express
yarn remove express
yarn run <script>
## More

We use Yarn to improve our development while keeping the workflow the same.

Anywho, I hope you learned something…

CTA: Check out my book on learning code

Happy coding!

Resources

Introduction to MongoDB: https://jessenerio.com/what-is-mongodb/

What is an API: https://jessenerio.com/how-an-api-works-and-why-they-are-so-cool/

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Search this blog

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Newsletter

Subscribe now to get my library of freebies + weekly emails with coding, tips for learning, and resource tips.

Learn to learn code faster, using techniques honed by years of study, neatly packaged into one book: How to Learn Code

Top content