How to Install Node.js
Several installation options are provided by Node.js to accommodate various operating systems and user preferences.
- Official Installers: For a lot of people, downloading the installer straight from
node.js.org
, the official website, is the easiest method. Linux, macOS, and Windows all have installers. Typically, a few clicks are required to go on to the installation procedures. The Node.js distribution is normally installed on Windows via the installer inC:\Program Files\nodejs
, and the required systemPATH
environment variable is configured. - Package Managers: Another integrated installation path is provided by system-specific package managers.
- macOS users can utilise Homebrew by running
brew install node
. Alternatively, Macports is also an option, usingsudo port install nodejs npm
. - Linux users, for distributions like Ubuntu, can install Node.js using
apt
withsudo apt-get install nodejs
andsudo apt-get install npm
. It’s worth noting that the Node and npm versions available viaapt
might be outdated. - Windows users can also use Chocolatey, a software management automation tool, with
choco install nodejs.install
. - Yarn, another package manager similar to npm, also lists installation instructions for various operating systems including macOS, Windows, and Linux distributions.
- macOS users can utilise Homebrew by running
- Node Version Manager (nvm): For developers who need to handle numerous Node.js versions on a single workstation, nvm is a popular and recommended solution. It’s a bash script that makes installing and navigating between Node.js versions easier. When testing your code with earlier Node.js versions or experimenting with new releases, this is quite helpful. You can use
wget
orcurl
scripts to install NVM. Installing global packages typically eliminates the need for sudo because nvm enables Node versions, packages, and other components to be installed in your home directory after installation.
Verify Installation
It’s important to make sure Node.js is configured appropriately after installation. You can use your terminal or command prompt to accomplish this.
- Verify the Node.js version: Simply type
node -v
on your terminal. This program will print the version of Node.js that is installed, such asv9.3.0
orv10.16.3
. It indicates a problem with the installation or PATH setup if you get a “command not found” error. - Check npm Version: Node.js installers include
npm
(Node Package Manager) from v0.6.3. Node.js’s default package manager is called npm. Online repository for Node.js packages and a command-line tool for installing, updating, and managing Node.js packages and their dependencies are among its features. Typenpm -v
ornpm version
to confirm that it has been installed.npm install npm@latest -g
updates earlier versions. - REPL (Read-Eval-Print Loop): By entering
node
into your terminal without any parameters, you can also access Node.js and check the installation. A>
prompt indicates that you have entered the Node shell, where you can type JavaScript code directly to see the outcome,2 + 2
to see the result4
. Typingconsole.log('test')
will showtest
followed byundefined
, confirmingconsole.log()
works and the function’s return value. To exit the REPL, you can type.exit
or pressCTRL+D
.
Your First Node.js Script: “Hello World!”
After installing and verifying Node.js, let’s build and launch your first Node.js application, a traditional “Hello World!” application.
- Create a
.js
file: Insert a new file into the directory that you have selected for your project (for example,my-first-node-app
). Common filename alternatives includehello.js
andapp.js
. Node.js code is entirely JavaScript, therefore make sure the file has a.js
extension. For example, you may writemain.js
orhello.js
. - Write the Code: In a text editor, open your freshly generated
.js
file and insert the following short line of code: - A key feature of Node.js (and JavaScript) is
console.log()
, which prints messages to the standard output, usually your terminal or command prompt. When this script runs, the phrase “Hello World!” will show up in your console sans the quotes. - Run the Script:
- Open your command prompt or terminal.
- Navigate to the directory where you saved your
.js
file. For example, usecd my-first-node-app
. - Execute the script using the
node
command followed by your filename
- Or
- The
node
command reads and runs your JavaScript file by calling the Node.js interpreter. - Observe the Result: Following the execution of the command, your terminal ought to display the following output:
- This straightforward operation shows how Node.js takes your JavaScript code, translates it into machine code, and then runs it. When developing more intricate Node.js apps, this fundamental process of creating files and executing them from the terminal is crucial.
You have successfully launched your first script after installing Node.js! The foundation for investigating more complex Node.js features, such building web servers, working with file systems, and using npm to manage packages, is this understanding.