المعرفة:: NodeJS الحالة::مؤرشفة المراجع:: The Complete Node.js Developer Course 3rd Edition, coggro’s Notes, anuragkapur’s Notes
Importing Node.js Core Modules
- All modules of Node.js API, listed in docs here, are available globally, so they can be used without importing them e.g.
console
.
Core Modules and require
- 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
fs
module. This is a built-in Node.js module that provides functions you can use to manipulate the file system. The script useswriteFileSync
to write a message tonotes.txt
. - 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.
Importing Our Own Files
require
can also be used to load in JavaScript files created by us. All you need to do is providerequire
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 calledutils.js
in thesrc
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
withmodule.exports
.
Exporting from Files
- 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 tomodule.exports
. The value stored onmodule.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.
- It’s possible to export and import more than one variable from a module too using Object Destructuring and ES6 short-hand for object literals.
- We can define a variable inside
modules.exports
directly too.
- Similarly, we can define a function.
Importing npm Modules
- 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.
Initializing npm
- 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 apackage.json
file in the root of the project.
Installing an npm Module
npm install
command can be used to install a new module in the project.
- The command does three important things:
- It creates a
node_modules
directory. npm uses this directory to store all the code for the npm modules you have installed. - It adds the module as a dependency by listing it in the dependencies property in
package.json
. This allows tracking and managing of installed modules. - npm creates a
package-lock.json
file. This includes detailed information about the modules you’ve installed which helps keep things fast and secure.
- It creates a
- You should never make changes to
node_modules
orpackage-lock.json
.
node_modules
directory shouldn’t be committed to git.
Importing an npm Module
- npm modules can be imported into your script using
require
. To load in an npm module, pass the npm module name torequire
.
- The script above uses
require
to load invalidator
. The script then uses theisURL
function provided byvalidator
to check if a given string contains a valid URL.
Global npm Modules
- 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.
- A globally installed module is not added as a dependency to your project. That means you won’t see it listed in
package.json
orpackage-lock.json
. You also won’t find its code innode_modules
. - Globally installed modules are located in a special directory in your machine which is created and managed by npm.
What is the difference between .js and .mjs files?
- https://stackoverflow.com/questions/57492546/what-is-the-difference-between-js-and-mjs-files.
- Common JS : uses
require
andmodule.exports
. - ECMAScript : uses
import
andexport
.
.cjs
: no matter whattype
is defined, we can use CommonJs syntax..mjs
: no matter whattype
is defined, we can use ES6 syntax.