Node.js comes with a built-in interactive shell known as the Read-Eval-Print Loop (REPL) terminal. The REPL allows you to interactively execute JavaScript code and see the results immediately. It is a helpful tool for testing small code snippets, experimenting with APIs, debugging, and learning JavaScript features.
To access the Node.js REPL terminal, follow these steps:
Open a terminal or command prompt on your computer.
Type node and press Enter:
node
After executing this command, you will enter the Node.js REPL terminal, and you will see a prompt indicating that you can start entering JavaScript commands:
Welcome to Node.js vXX.XX.XX.
Type ".help" for more information.
>
The version number (e.g., XX.XX.XX) will depend on the installed Node.js version.
Start entering JavaScript code: You can now enter any JavaScript code at the prompt, and Node.js will evaluate and execute it immediately. For example:
> 2 + 3
5
> const message = "Hello, REPL!";
undefined
> console.log(message);
Hello, REPL!
undefined
The REPL will print the output of the code right after you press Enter. If the code assigns a value to a variable, it will display undefined.
Use .help for assistance: The Node.js REPL has some built-in commands to assist you during your session. For example, you can use .help to get a list of available commands:
> .help
.break Sometimes you get stuck, this gets you out
.clear Alias for .break
.editor Enter editor mode
.exit Exit the repl
.help Print this help message
.load Load JS from a file into the REPL session
.save Save all evaluated commands in this REPL session to a file
Exit the REPL: To exit the Node.js REPL, you can either use the .exit command or press Ctrl + C twice.
The Node.js REPL is a handy tool for quickly testing code snippets and learning JavaScript concepts without the need to create a complete Node.js application. It's particularly useful for experimentation and exploration.
Node Package Manager (NPM) is a powerful tool that comes bundled with Node.js. It is the default package manager for Node.js, and it allows developers to install, manage, and share packages or libraries with other developers. NPM is used to handle project dependencies and streamline the process of integrating third-party modules into Node.js applications.
Here are some key concepts and common NPM commands:
Package.json: It is a file in the root of your Node.js project that contains metadata about the project and a list of dependencies required for the project. You can create a package.json file by running npm init, and it will guide you through setting up your project details.
Installing Packages: To install a package, use the npm install command followed by the package name. For example, to install the Express web framework:
npm install express
By default, packages will be installed in the node_modules directory in your project.
Dependency Management: When you install a package using npm install, NPM will automatically update your package.json file with the installed package and its version in the dependencies section. This ensures that anyone else who clones your project can install the same packages with the correct versions.
Global vs. Local Install: By default, packages are installed locally for a specific project. However, you can also install packages globally, which makes them available system-wide. Global packages are typically used for command-line tools that you want to access from anywhere in your system. To install a package globally, add the -g flag to the npm install command:
npm install -g package-name
Running Scripts: You can define custom scripts in your package.json file under the "scripts" section. These scripts can be executed using the npm run command. For example, if you define a script called "start," you can run it with:
npm run start
Updating Packages: To update a package to its latest version, use the npm update command followed by the package name. To update all packages in your project, simply run npm update.
npm update package-name
Uninstalling Packages: To remove a package from your project, use the npm uninstall command followed by the package name.
npm uninstall package-name
Searching for Packages: To search for packages on the NPM registry, use the npm search command followed by the package name.
npm search package-name
These are some of the most commonly used commands and concepts related to NPM. It's worth noting that NPM is constantly evolving, and you can always find the most up-to-date documentation on the official NPM website: https://docs.npmjs.com/.