JavaScript has been present in the software world since 1995 and has since played a dominant role in web development. For much of its lifespan, JavaScript has been primarily used to create front-end scripts within
IS NODE.JS A FRAMEWORK?
Node.js is not a framework. Instead, Node.js is a JavaScript runtime environment. You can think of it as a place where you can run JavaScript without adding code to HTML.
This limitation meant that web software developers often worked with many different languages and frameworks in both the front-end (client-side) and back-end (server-side) aspects of a web application.
const fs = require('fs');
fs.readFile('./script.js', function(error, data) {
// If no error occurs, the error will be null; otherwise, it will be an Error object.
if (error) {
throw error;
}
console.log(data);
});
How to Use Node.js
To get started, download and install Node.js from here, suitable for your operating system.
To run JS files in Node, use the ‘node
‘ command followed by a file path, and it will execute the program file.
For example, if we have saved the following in a script.js file:
console.log('I am a Node program');
To run the terminal command with script.js in the same folder using ‘node script.js,
‘ it will initiate Node, print ‘I am a Node program
‘ to the terminal window, and exit after completing the script execution.
Node as a REPL
Node.js can also be used as a Read-Eval-Print Loop or REPL in a terminal window. This functionality allows you to execute JavaScript commands from the command line.
Once Node is installed, you can initiate the REPL by running the ‘node
‘ command in a terminal and pressing the ‘Enter‘ key. Now, you are in an interactive JavaScript environment, and you can run any valid JavaScript code such as 4 + 5.
After executing a command, Node will always print the result of that evaluation.
$ node
4 + 5
9
function nodeIsGreat() {
… console.log('Node is great!');
… }
undefined
nodeIsGreat()
Node is great!
undefined
.exit
$
In this example, the user initiates Node with the ‘node
‘ terminal command in the first line. In the second line, the user types ‘4 + 5
‘ and evaluates it by pressing the return key, which prints ‘9
‘ to the exit terminal.
In the fourth line, the user declares a function named ‘nodeIsGreat
.’ Since this function declaration spans multiple lines, the REPL Node will indicate with ‘…
‘ at the beginning of each line that it is still reading the user’s input and hasn’t been evaluated yet. After closing the function declaration in the sixth line, it prints ‘undefined
‘ to the exit terminal because the function declaration itself is not evaluated as a value. When the function is called in the eighth line, ‘Node is great!
‘ appears in the console, and ‘undefined’ is logged afterward because ‘nodeIsGreat()
‘ returns ‘undefined
.’
To exit Node REPL, use the ‘.exit
‘ command at any point and return to the system prompt. Pressing ‘ctrl+c’ twice will also provide an exit simultaneously.
Loading Existing Files
Node REPL can also load existing JS files. If we had saved the following code in a file named script.js:
var a = 'Node REPL is fun!';
You can use .load
to load into the REPL. .load
takes a file path argument, so to load script.js
, you would use .load ./script.js
.
$ node
> .load ./script.js
var a = 'Node REPL is fun!';
> a
'Node REPL is fun!'
After loading the script file, variables can be accessed from the REPL. So, when we evaluate the value of the variable ‘a,’ it has been set by loading into script.js and prints ‘Node REPL is fun!’ to the console.
You can try this by running node
in the terminal yourself, or refer to the REPL documentation for more functionality.
Package Management
Node packages are an easy way for Node developers to share modules. Npm service is the default package manager for Node and comes with the Node.js installation.
Npm allows access to hundreds of thousands of open-source packages. In addition to npm, yarn has gained popularity as another JS package manager.
Node Versions
Node’s major versions aim to support the latest JavaScript features, including ES6/ES2015 and beyond, without transpilation.
To see a list of supported JavaScript features in different versions, you can visit node.green.
Version Management
As with any major software, top-level Node versions (8.x, 7x, etc.) sometimes introduce significant changes that may affect applications built on older versions. Version managers like nvm (Node Version Manager) and n can be used to switch between multiple Node versions on a single machine.
There are two version managers that provide this functionality: nvm (Node Version Manager) and n. Installing n as an npm package is quite straightforward!
You can also learn more about Node.js in the following video.