what is node.js? How is it different from javascript?

by Stephanie Coates

When starting out, I had a lot of confusion about what Node.js actually was. Is it the back-end-language version of JavaScript? Is it a webserver? Is it a command line tool?

At the root level: the creators of Node.js took JavaScript, which before was only able to run in the web browser, and gave it the ability to run outside the browser on actual computer hardware.

This was done copying Google Chrome’s V8 JavaScript engine and running it on a server.

At the basic level, a JavaScript engine is a program that reads/parses a JavaScript file, converts/compiles the script into machine language, and runs it as machine code. In web browsers, the embedded JS engine is sometimes called a JavaScript virtual machine.

Before, only web browsers had the ability to parse JavaScript, since they were the only programs that contained JavaScript engines. Thanks to the creation of Node.js, any server that has Node.js installed is able to understand JavaScript as well.

Node.js as a web server

Aside from window and document objects used in front-end JS development (Node.js provides global and process objects instead), the Node.js runtime can parse any valid JavaScript. A server-side runtime also adds functionality to:

  • access files on your machine
  • listen to network traffic on your machine
  • listen to incoming HTTP requests that your machine receives, and send back responses
  • access databases directly

Node.js is built on a single-threaded, non-blocking event loop, the V8 JS engine, and a low-level I/O API. It’s become super popular for building fast HTTP servers (aka web servers) with the Express web framework, ideal for high-traffic, data-intensive (but low CPU) applications. Like chat apps.

Anything that could be done with PHP or Ruby on Rails can now be done with JavaScript, thanks to Node.js.

Node.js for local utilities

npm

In addition, Node.js can provide a better local development experience. Node.js comes with npm, or Node Package Manager, where you can download and manage packages from a huge ecosystem of third party libraries. When you run npm install <npm-package-name>, Node.js downloads the package, adds it to the node_modules folder in your project, and adds the library as a dependency in your project’s package.json.

When you’re just creating a client-side application, Node.js and npm are solely to provide a better developer tooling. Once your code is compiled and built (using npm scripts), the code is no longer reliant on Node.js to run. It can be hosted as a static relying on any back-end language.

npm scripts

If you’re building your client-side application with React and use create-react-app, npm scripts are set up out-of-the-box with react-scripts.

  • npm start uses Webpack to launch a hot-reloading dev server available at localhost:3000
  • npm test launches the jest testing framework
  • npm run build uses Webpack to create a production-ready version of the app, output in the build folder
  • npm run eject removes the single build dependency from your project and allows you to customize configuration for Babel, Webpack, ESLint, etc.