Globals node-specific functionality
node provides access to several important global objects for use with Node program files. When you write a file that will run in a Node environment, these variables will be accessible at the global scope of your file.
- module is an object that refers to the functionality to be exported from a file. In Node, each file is treated as a module.
- require() is a function used to import modules from other Node files or packages.
- is an object that refers to the actual computer process that runs a Node program and allows access to command-line arguments and more.
Node has many built-in modules to aid in interactions with the command line, the computer’s file system, and the Internet. These include
HTTP and HTTPS for
- creating web servers
- path to interact with the file system, operating system, and file/directory paths.
. File system, operating system, and
You can view the full documents to see more of Node’s built-in features.
To handle asynchronous code, Node uses a system based on callback. Node functions and methods that will implement some asynchronous activity take a callback function. This callback will be called as long as the asynchronous operation has been resolved. By convention, the first argument of this callback is an error placeholder. If the asynchronous operation failed (trying to read a nonexistent file, for example), the error argument will be an Error object, but it will be null if no error occurs.
In this example, we are using Node’s built-in fs module to read a .js script file. The callback function is called after the file read operation is completed. If an error occurs, it will be passed as an error and launched. If it does not exist, the data retrieved from the file read operation is logged in the console.
How to use
This video will show you how to download and install
get started, download and install Node.js for your operating system
To run JS files in Node, the node command followed by a file path will execute the program file.
For example, if we have the following saved in a file script.js:
When running the terminal command node script.js in the
same folder as script.js Node will start, I will print I am a node program in the terminal window and exit, since the script file has finished execution
. Node as REPL node It can also be used in a terminal
In this example, the user starts the node on line one with the node terminal command. On line 2 the user types 4 + 5 and evaluates with the return key. 9 prints in the output terminal.
On line 4, the user opens a nodeIsGreat function declaration. Because this function declaration takes several lines, Node REPL will print… at the beginning of a line to show that you are still reading the user input instruction and have not yet been evaluated. After you close the function declaration on line 6, undefined is printed on the output terminal, because the function declaration itself does not evaluate to any value. When the function is invoked on line 8, Node is great! records in the console and records not defined later, because nodeIsGreat() returns undefined.
To exit the
REPL node, use the .exit command at any time and return to the system shell. Pressing ctrl + c twice will also close it.
The REPL node can also upload existing JS files. If we had the following code saved in
script.js: We can use .load to load
it into the REPL. . load takes a path argument, so to load script.js we would use .load ./script.js.
After loading the script file, the variables can be accessed in the REPL,
so when we evaluate the variable, its value has been set by loading script.js, and
‘Node REPL is fun!’ is printed in the console.
Try it yourself by running the node in a terminal or refer to the REPL documents for more functionality. Management Node
packages are a convenient way to share modules between Node developers. The npm service is the default package manager for Node and is included with a Node installation.
NPM allows access to the hundreds of thousands of open source packages available.
In addition to npm, the thread is gaining popularity as another JS package manager.
To learn more and see npm in action, take our Browser Compatibility and Transpilation course.
Major versions of Node
As with any major software release, top-level Node versions (8.x, 7x, and so on) sometimes introduce major changes to applications created in earlier versions of the environment. A version manager can be used to switch between multiple versions of Node on a single computer.
There are two version managers that provide this functionality: nvm (Node Version Manager) and n. N can be installed very easily as an npm package!