Node.js comes with dozens of built-in modules. These built-in modules, sometimes referred to as core modules, gives access to tools for working with the file system, making http requests, creating web servers, etc.
The module system is built around the require function. This function is used to load a module and get access to its contents. require is a global variable provided to all your Node.js scripts.
The script above uses require to load in the fsmodule. This is a built-in Node.js module that provides functions you can use to manipulate the file system. The script uses writeFileSync to write a message to notes.txt. ^cf1e9e
Note that we can have a different name for the variable fs and the program would work, though it’s common to stick with the convention for all node modules we load. ^2adeeb
require can also be used to load in JavaScript files created by us. All you need to do is provide require with a relative path to the script you want to load. This path should start with ./ and then link to the file that needs to be loaded in.
The code above uses require to load in a file called utils.js in the src directory. It stores the module contents in a variable, and then uses the contents in the script.
Each module has its own scope with its own variables because Node.js scripts don’t share a global score. This means variables created in one scripts are not accessible in a different script. The only way to share values between scripts is by explicitly export what we need to use using require with module.exports.
Node.js provides the required script with a place to store values that should be exported as part of the library which is module.exports.
In utils.js below. A function is defined and then assigned to module.exports. The value stored on module.exports will be the return value for require when the script is imported. That means other scripts could load in the utilities to access the check function.
NPM is a package manager that allows you to install and use third-party npm libraries in your code. This opens up a world of possibilities, as there are npm packages for everything.
A Node.js application needs to initialize npm before it can be used.
Run npm init from the root of the project to get that done. That command will ask you a series of questions about the project and it’ll use the information to generate a package.json file in the root of the project.
1
2
3
4
5
6
7
8
9
10
11
{"name":"notes-app","version":"1.0.0","description":"","main":"app.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"author":"","license":"ISC",}
The script above uses require to load in validator. The script then uses the isURL function provided by validator to check if a given string contains a valid URL.
You can use npm modules from outside of your scripts by installing them globally.
npm modules can be installed globally by adding a -g flag to the installation command.
Not all modules are designed to be installed globally.
1
2
# install as a global utility - doesn't add to the project specific package.json npm install <package-name> --global
A globally installed module is not added as a dependency to your project. That means you won’t see it listed in package.json or package-lock.json. You also won’t find its code in node_modules.
Globally installed modules are located in a special directory in your machine which is created and managed by npm.