Introduction
Node.js is a run-time environment used for executing JavaScript outside the browser. Npm is a JavaScript package manager commonly used for installing packages for Node.js.
In this article, you will learn how to install Node.js and npm on Mac or Linux. You will also learn how to verify the installation, run Node.js code, and how to update Node.js and npm.
When you’re finished, you’ll be able to run Node.js code on Mac or Linux computer.
Installation on a Mac or Linux. In order to install everything on a Mac, we'll be running commands in Terminal.app, and Linux distributions vary. Install Node.js and npm. We’re going to use Node Version Manager (nvm) to install Node.js and npm. When installation is complete, open the Terminal by pressing Command+Space to open Spotlight Search and entering Terminal then pressing Enter. Enter node - v in the Terminal to verify that Node.js is installed correctly and to see the version of Node.js that was installed. Nats-MacBook-Pro:Webucator natdunn$ node.
Prerequisites
To follow this tutorial successfully, you’ll need the following:
- Internet access to download Node.js.
- Basic knowledge of how to use the terminal.
Step 1 — Install Node.js and npm on Mac or Linux
There are a couple of ways to install Node.js on Mac or Linux. The most recommended method and also my favorite method to install Node.js and npm is to use Node Version Manager(nvm).
Using nvm has the following benefits:
- It allows you to install multiple versions of Node.js.
- It makes it easy to switch between the versions.
- Updating node.js is a breeze with nvm.
So let’s now install nvm. Copy and paste the command below into your terminal.
Info: If your on Mac, open your
~/.bash_profile
or create a new one if the ~/.bash_profile
file does not exist. Make sure you add source ~/.bashrc
in the file and save.Now let’s verify if
nvm
was installed successfully.Your terminal should display the current version of nvm. At the time of writing, the current version is 0.35.2. So this is the output I got.
Note: On Linux, if you get
nvm: command not found
or no feedback, close your terminal and open a new terminal and enter the nvm --version
again. If you using ssh to access linux, you might need to restart your session.Tip: On Mac, if you are having any issues with the
nvm
command not working, check out this linkLets now install Node using nvm’s install command.
Once installed, set the installed version as the default.
Info: You need to set the default version because nvm forgets the node version in a new terminal session. You can learn more about the issue here.
Step 2 — Verify Node.js and npm installation
Verify if Node.js and npm were successfully installed.
Type
node -v
. It will display the current Node.js version.Type
npm -v
to display the current npm version.Your output should look similar to mine. Note that the versions will be different since you might be reading this tutorial when new versions came out.
Running Node.js Code on Mac or Linux
There are two ways to run Node.Js code:
- Node.js REPL
- Running Node.js scripts on the terminal
Node.js REPL
REPL stands for Read-Evaluate-Print-Loops. It is an interactive shell that allows experimentation of JavaScript code. It works the same way as the web browser console.
To access the Node REPL, type
node
in the terminal.The REPL prompt will be opened where you can enter the JavaScript code.
The
>
sign is there to let you know that the code entered will be immediately evaluated when you press enter.To exit out of the REPL, type
.exit
or press CTRL+D
once. You can also exit out by pressing CTRL+C
twice.Running Node.js Scripts on the Terminal
This method is the one that is commonly used to run Node. You will write Node.js code in a file with an extension of
.js
and run it in the terminal using the node
command.Let’s see this in action by creating a directory named
node.js
in our home directory.mkdir
is a command used for creating directoriesLet’s go into the
nodejs
directory.Your terminal should now show you are in the
node.js
directory.Create a file
first-program.js
. For simplicity’s sake, we will use Nano which is a terminal text editor.Enter the following code in
first-program.js
in the Nano editor.Hold CTRL+O and then press
enter
to save the file.Exit out of the editor by holding
CTRL+X
.Let’s now run the code in
first-program.js
:Your output should look like below.
I hope you have understood how to run Node.js code on your Mac or Linux system.
Next, we are going to learn how to update Node.js and npm using nvm.
How to Update Node.js and npm using nvm
To update to the latest version Node.js and npm. Type the following:
To update to the latest LTS(Long Term Support) version. Type the following:
Conclusion
In this article, you learned how to install and update Node.js and npm on Mac or Linux. We also looked at different ways on how to run Node.js code. I hope you are now confident to run Node.js scripts on your computer on your Mac or Linux computer.
If you have any insights or suggestions, feel free to leave a comment. I would love to hear from you.
Here’s how I get productive for JavaScript/Node on Mac OS.
It includes iTerm2, zsh, Node, Visual Studio Code and some git commands.
Table of Contents
Setup iTerm2
- Install iTerm2
Change edit mode to natural text
- iTerm Preferences → Profiles → select your profile → Keys tab → Load Preset… → Natural Text Editing (See this StackOverflow answer)
New session should start where previous left off
- iTerm Preferences → Profiles → select your profile → General tab → Working Directory section → Reuse previous session’s directory option
Quit on tab close
- iTerm Preferences → General, “Closing” → “Quit when all windows are closed”
Increase font size
- iTerm Preferences → Profiles → select your profile → Text tab → Font section → Change font → Update font in the popup
- Fan of 16pt Monaco (12, 14 is just too small)
Enable infinite history
- iTerm Preferences → Profiles → select your profile → Terminal tab → Unlimited scrollback
Shell setup
zsh
With oh-my-zsh manager. Sets you up with auto-completion.
snazzy colour theme
Using iterm-snazzy, which is a case of downloading the
.itermcolors
file and choosing the theme from (iTerm Preferences → Profile > Colors > Color Presets…).Pure prompt
It’s simple, clean but gives you enough information to be productive.
(see Setup and configuration for how to get Node/npm up and running)
- Install using npm:
npm install --global pure-prompt
- Initialise by adding the following to your
.zshrc
:
As a developer it’s always good to have a few browsers and tools handy:
- Google Chrome: still a goto due to its solid and extensive dev tools. Usually I install the React or Vue dev tools.
- Postman for Mac: to manually test APIs
- Firefox: number 2 browser
- Brave: auto-blocks ads and tracking, sort of the “play” browser, its dev tools are a buggier/less ergonomic version of Chrome dev tools (this is because Brave uses Chromium under the hood)
- Safari - installed by default on Mac OSX, it’s a buggy browser, good to test using it since it surfaces weird SVG and cookies security policy quirks. Since it’s the default it’s also widely used by non-technical people.
- Enable the dev tools: Safari → Preferences → Advanced → Show develop menu in menu bar.
I use Visual Studio Code, it strikes the right balance between usable out of the box and customisable. The way I see it editors like vim or Atom need a bit of config before being productive, and others like Sublime or IDEs (WebStorm) don’t have the same plugin ecosystem.
Install VSCode command line tools
Open the dialog using CMD + P.
Use:
Shell Command: Install 'code' command in PATH
The VSCode command line tool usage examples:
code .
: open.
directory in VSCodecode -r .
: replace directory opened in VSCode with the current directorycode -a .
: add current directory to VSCode, ie. initialises a workspace
Must-have extensions
- Atom keymap: I’m not a fan of the default keybindings, this uses Atom-style ones, get it from the Visual Studio Marketplace or
ext install atom-keybindings
fromCMD + P
menu - EditorConfig for VS Code: “EditorConfig helps developers define and maintain consistent coding styles between different editors and IDEs.” (see editorconfig.org), ie. helps you deal with tab size, trimming spaces etc. across code editors, get it here from the Visual Studio Marketplace or
ext install EditorConfig
fromCMD + P
menu
Nice to have extensions
- ESLint: “Integrates ESLint JavaScript into VS Code.”, get it from the Visual Studio Marketplace or
ext install vscode-eslint
fromCMD + P
menu - npm Intellisense: “autocomplete npm modules in import statements”, get it from the Visual Studio Marketplace or
ext install npm-intellisense
fromCMD + P
menu - Snazzy theme: same colour theme (snazzy) as I’ve got setup for the terminal for VSCode, get it from the Visual Studio Marketplace or
ext install snazzy theme
- Import Cost: “Display import/require package size in the editor”, get it from the Visual Studio Marketplace, or
ext install import-cost
Not many productivity apps, just Alfred, which I use as a better Spotlight Search and Clipy which is a clipboard manager.
Maccy
A simple clipboard manager designed for OSX.
It works out of the box better than Clipy (see below).
Install it through Homebrew:
Update the preferred hotkey to
CMD + shift + v
:Paste by default on selection of a clipboard item:
Superseded by Maccy Clipy
Bump up the number of “inline items” (Clipy → Preferences → Menu → Number of items place inline).
Install Node Js Mac
Set your screenshots to save to clipboard + enable the option to paste as plain text (Clipy → Preferences → Beta → Paste as PlainText + Save screenshots in history).
Update Xcode using
xcode-select
--``install
.Install Homebrew for package managements (think apt or pkg for Mac):
Install Node.js either from source, using the Mac installer or using Homebrew:
Install
n
– Interactively Manage Your Node.js Versions using npm (now that we have Node installed):npm install
--``global n
Switch to latest Node version using
n
:sudo n latest
Install jq (format and deal with JSON nicely in the terminal) and watch (run a command repeatedly) using Homebrew
Add a few git extensions:
- git-open: “Type
git open
to open the GitHub page or website for a repository in your browser.” usingnpm install --global git-open
- git-standup: “Recall what you did on the last working day.” using
git standup
, there are multiple install options (see git-standup#install), I usually go with:brew install git-standup
- git-lg: simpler/prettier
git log
:
Add the following minimal
.vimrc
, which enables syntax highlighting, has basic tab/tabsize configuration and enables line numbers display:Set up SSH keys and add to VCS hosting, see this GitHub help article:
- Generate a new key:
ssh-keygen -t rsa -b 4096 -C '*[email protected]*'
- Copy your public key to clipboard so you can paste it wherever your hosted Version Control system asks you to:
pbcopy < ~/.ssh/id_rsa.pub
Put percentage on power level, right-click the battery indicator and select “Show Percentage”.
❤️ Spotify
More at my /uses page.
(Optional) Docker, VirtualBox
Docker is a containerisation technology, think VMs but smaller. I recommend Docker for Mac.VirtualBox allows you to run Virtual Machines on Mac, install it at VirtualBox downloads.
Get The Jest Handbook (100 pages)
Take your JavaScript testing to the next level by learning the ins and outs of Jest, the top JavaScript testing library.
Node Js Mac
orDownload Node Js Mac Terminal Download
Join 1000s of developers learning about Enterprise-grade Node.js & JavaScript