Node JS
Overview
An environment to run JS outside of the browser.
Open source runtime.
Built on Chrome's V8 JS engine.
The ecosystem has vastly improved since it was created. Developers can now build almost anything with only JS. The tooling surrounding Node is incredible and outperforms many other languages.
While Node is suitable for most web applications/websites today, it's not meant for highly intensive CPU activities (single-threaded with an event loop).
Node is great for creating:
Tooling - build or automation
API (REST, real-time)
CDNs
Shareable libraries (
npm
)Desktop appplications (Electron) with web technologies
IOT
Globals
process
: information about the environment the program is running in
require
: function to find and use modules in the current module, Node's way of importing
__dirname
: the current directory path
module
: information about the current module, methods for making module consumable
global
: like window
but for Node, does not provide access to browser APIs
Modules
Encapsulated JS code. They look like this:
Node will pass these globals in an IIFE for you to be able to use.
Until Node migrates over to ES6 modules, CommonJS is the only way to share code (without transpiling the code).
Creating Modules
This allows us to separate the actual code and the exports (revealing module pattern).
Being explicit about what you're exporting is very important for bundling tools for better performance (i.e. tree-shaking).
Using Modules
require
needs a relative path when you import a file that you created. There are other ways to import using require
when you import an external module. It's a dependency tree that Node resolves.
Internal Modules
Modules that come shipped with Node. Some of the frequently used ones include:
fs
: fileSystem module for interacting with files on a machinehttp
: low level-ish module for creating network-based programs like APIspath
: for manipulating path strings and handling differences across Operating Systems
Last updated