Node & Node Modules
Last updated
Last updated
Table of Contents:
In this lesson we'll learn the history of Node and the fundamentals of using Node Modules to build JavaScript scripts.
Node is a "JavaScript runtime environment" which is just a fancy way to say it is a program for running JavaScript.
A module is an exported chunk of code (typically a function or a set of functions) that can be used across our project.
In a Node project, a file exports a module by assigning a value to module.exports
A file can export a module in two ways:
It can export one value/function (a single "default export")
It can export many values/functions (a group of "named exports")
A module can be imported with the require(filename)
function.
Modules can be downloaded from the Node Package Manager (NPM) online registry
When a module is downloaded, it is called a dependency
package.json
is a file with meta-data about a Node project including dependencies and configuration.
JavaScript Object Notation (JSON) is a file format for representing data in an JavaScript-Object-like notation with key:value pairs.
Developer dependencies are dependencies used by the developer(s) who created the package but aren't needed by the users of the package. They are not added to the node_modules
folder of the user.
JavaScript started out as a language that could only be executed by a browser.
In 2009, Node was invented to allow programmers to run JavaScript on their own computers without a browser, opening the door for fullstack JavaScript development.
Node is a "JavaScript runtime environment" which is just a fancy way to say it is a program for running JavaScript.
To run a JavaScript program, we use the command node <file_name>
If you use the node
command on its own, you will open the Node Read Evaluate Print Loop (REPL) program.
Consider the simple JavaScript program below. It declares a few functions for calculating data about a circle with a given radius and then prints them out.
Notice that the main
function just uses the other functions.
JavaScript projects are rarely built entirely in one file like this. Instead, code is separated into multiple files that share code with each other.
These shared pieces of code are called modules. A module is an exported chunk of code (typically a function or a set of functions) that can be used across our project.
For example, a file can create a function and then export it.
Meanwhile, another file can import that function and use it.
Modules help with organization and separation of concerns but require more files!
module.exports
(CommonJS)In a Node project, a file exports a module by assigning a value/function to module.exports
.
When module.exports
is assigned a single value/function, we call that a default export.
You will also commonly see module.exports
be assigned to an object containing multiple values/functions. These are called named exports:
require()
(CommonJS)To import a value/function exported from another file, use the require(filepath)
function and provide a filepath
argument.
If the exported value is an object, we typically will destructure the object immediately upon importing it:
Modules can be downloaded from the Node Package Manager (NPM) online registry.
When you download a package, it is called a dependency.
Visit https://www.npmjs.com/ to explore available packages. Start by searching up the "prompt-sync" package.
To install any module from npmjs, use the npm install
(or npm i
) command in your Terminal:
Now, you should see a node_modules/
folder with a prompt-sync/
folder inside. Open up the prompt-sync/index.js
file to see the module that is exported!
To use the package in our own program, use require()
again, this time with just the name of the module.
package.json
and node_modules
Every dependency of a project, and its version number, will be listed in the file package.json
(if the file doesn't exist, the npm i
command will create it). The existence of this file turns our project into a package.
JavaScript Object Notation (JSON) is a file format for representing data in an JavaScript-Object-like notation with key:value pairs.
The downloaded module will be placed in a folder called node_modules/
along with any sub-dependencies that the module itself may require.
You can see the sub-dependencies of a module by opening its own package.json
file. All modules listed under "dependencies"
will also be installed in node_modules/
.
In prompt-sync/package.json
, we can see it has strip-ansi
as a dependency.
In strip-ansi/package.json
, we can see it has ansi-regex
as a dependency.
In the prompt-sync/package.json
file, you will notice that prompt-sync-history
is listed under "devDependencies"
.
Developer dependencies are dependencies used by the developer(s) who created the package but aren't needed by the users of the package. They are not added to the node_modules
folder of the user.
Try installing the nodemon
module as a developer dependency using the npm i -D
command:
You should see nodemon
added to the "devDependencies"
section of package.json
(version numbers may vary):
The nodemon
module installs a new command nodemon
that can be used to run a JavaScript file in "hot reload" mode. This means that any time you save a file in the project, it will re-run the file.
package.json
Scripts and nodemon
You can add a "scripts"
section to the package.json
file to make it easier to run commonly used Terminal commands.
Two common script commands to add are "start"
which runs node index.js
and "dev"
which runs nodemon index.js
To use these commands, we can type npm run script_name
. For example, npm run start
or npm run dev
npm init -y
When working on a new Node project, it is common to set up the package.json
file prior to installing any dependencies.
This can be done using the command npm init
which will ask you some questions to generate a package.json
file.
You can also run npm init -y
to skip the questions and build a package.json
file using default values.
A program is considered hard-coded if the program code must be modified in order to produce a new result.
The prompt-sync
function is really useful for creating programs that will produce new results depending on the user's input.
In the 6-madlib-challenge/index.js
file you will find the following hard-coded program:
Your goal is to do the following in the 6-madlib-challenge
folder:
Download the prompt-sync
module using npm i prompt-sync
Import and configure the prompt
function in index.js
Replace the hard-coded variables defined in the main
function with values retrieved from the user via the prompt
function.
Re-organize the code such that the madlib
function is in its own file called madlib.js
that exports madlib
as the default export.
If you get stuck, you can view the solution below: