For enquiries call:

Phone

+1-469-442-0620

HomeBlogWeb DevelopmentHow to Install Node.JS on a Mac

How to Install Node.JS on a Mac

Published
05th Sep, 2023
Views
view count loader
Read it in
7 Mins
In this article
    How to Install Node.JS on a Mac

    Node.js is an opensource JavaScript platform for general-purpose programming that allows users to quickly build network applications. As it uses JavaScript on both the front and backend, development becomes far more consistent and integrated. 

    Node.js runs on various platforms (Windows, Linux, Unix, Mac OS, etc.) In this tutorial, we will discuss about Node.js installation on macOS.

    As we have already mentioned, Node.js allows you to write JavaScript on the server-side.  JavaScript, as you know, is a browser-based language. The creator of Node.js took the engine of Chrome and set it to work on a server. The browser's engine compiles JavaScript code into commands, and the language can be interpreted in an environment.   

    Npm is the platform for Node.js package management. It offers a tool for Node.js libraries to be installed, and their versions and dependencies managed.

    Prerequisites

    Hardware Requirement:

    • RAM: 4 GB 
    • Storage: 256 GB of Hard Disk Space

      Software Requirements:

    • Web Browser: Any browser such as Google Chrome, Mozilla Firefox, Microsoft Edge, Safari.
    • XCode: XCode software is used by Apple in building Mac and iOS applications, so it provides the instruments you need to compile Mac software. You can find XCode in the Apple App Store.  
    • Homebrew: Homebrew is a package manager for the Mac. It readily allows the installation of most open-source software (like Node).  On the Homebrew website, you can find out more about Homebrew.
    • Operating System: macOS

    Installation Procedure

    In this article, we are going to look at three different ways to install Node.js on macOS.

    1. using the macOS installer  
    2. using homebrew  
    3. using Node Version Manager

    So, let us start.

    1. Using the macOS installer

    Step 1: Visit the Node.js website to update your Mac platform with the built installer.

    Node.js updates comprise two types, long-term support (LTS) and new releases. LTS versions are refined and bug-free, and are sufficient for most daily users. Current versions (Latest LTS 14.16.1) are more experimental and include the new functionality, which cannot be completed or crashed from time to time. By highlighting the field on the first tab, you can move between LTS and current versions. Again, most users use the LTS version. Therefore, you can just click on macOS installer, which will download the Node.js installer, if the LTS tab is highlighted in dark green.

    Step 2: Download .pkg installer and open the downloaded file with default installer.

    Step 3: After running the .pkg installer, follow the instructions on the interface.

    1. Introduction window: select Continue

    2. Licence window: Select Continue and a pop-up window will ask you to agree or disagree. Click on Agree to proceed.

    3. Select Install and Authenticate your macOS password.

    4. You will see that Node.js and npm are installed on the interface.

    Step 4: Verify the installation of Node.js and npm by using the following commands on the terminal:

    node -v
    npm -v

    Using homebrew to install and update Node.js

    Though there are a lot of features in the command line interface of macOS, Linux and other Unix systems do not have a decent package manager. A Package Manager consists of a series of software tools that automatically install, configure and update the software.

    They manage the software in a central location and maintain all the device software packages in widely used formats. The Homebrew software package management framework is free and open-source and simplifies the installation of macOS software. The latest version of Node.js can be installed using Homebrew.

    Step 1: Use the following command to install Homebrew:

    s$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

    When CURL downloads the script, the Ruby-interpreter starts the Homebrew installation process, which ships with macOS.

    During the operation, you will be asked to enter your password. While you don't see them, the machine records your keystrokes, so once your password has been entered, click the RETURN key.

    Step 2: Once homebrew is installed, use the following command to install Node.js:

    $ brew update
    $ brew install node

    Step 3: Verify the installation and check the version of Node.js and npm.

    $ node -v
    $ npm -v

    Homebrew always installs only the latest version of Node.js. This may be problematic since a certain version of Node.js might be required to function in applications. It can be a good thing to have the freedom to use those versions. The easiest way to solve this problem is by using NVM, the Node Version Manager.

    Using nvm to install and update Node.js

    NVM is a bash script for several active versions of Node.js. Follow these steps to install Node.js:

    Step 1: The script copies the nvm-repository to ~/.nvm and then attaches the sources to the profile of the shell: ~/.bash profile, ~/.zshrc and ~/.profile or ~/.bashrc. Depending on what you have on your machine you can use curl

    Use the following command:

    $ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

    Step 2:  Add the source lines in your shell profile. You can use ~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc. In this article, we will be using zsh shell:

    $ vim .zshrc

    Step 3: Paste the following lines of code:

    export NVM_DIR="$HOME/.nvm"[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" [ -s "$NVM_DIR/bash_completion" ] && \. "\$NVM_DIR/bash_completion"

    Close the file by using esc + w + q

    Step 4: Use the following command to reload the shell:

    $ source ~/.zshrc

    Step 5: You can verify the installation with the following command and if the screen shown had nvm written as the output, then the installation is successful.

    $ command -v nvm

    Note: You can check the various commands of nvm with the following command:

    $ nvm + tab key

    Step 6: Install LTS version:

    $ nvm install –lts

    Step 7: Install the latest version of node:

    $ nvm install node

    This is an alias for the latest version.

    Step 8: You can list out all the installed Node.js versions available on your system:

    $ nvm list

    Step 9: Install NPM:

    $ nvm install-latest -npm

    Step 10: This installs the latest NPM. After you've set up NPM, you can try out a number of useful commands:

    List and update globally installed packages:

    $ npm ls -g  --depth=0
    $ npm update  -g

    Create a simple Program

    Let's create a simple program "Hello, world." This ensures that our environment works and that you can build and run a Node.js program conveniently.

    Step 1: In order to load the http module and store the returned HTTP instance in a http variable, we use the directive:

    var http = require("http");

    Step 2: To build a server instance, use the created http instance and call http.createServer() and connect it to port 8081 through the servers instance. Pass a function with request and response parameters. We will print Hello World!

    http.createServer(function (request, response) {
       // Send the HTTP header  
       // HTTP Status: 200 : OK
       // Content Type: text/plain
       response.writeHead(200, {'Content-Type': 'text/plain'});
       response.end('Hello World\n');
    }).listen(8081);
    console.log('Server running at http://127.0.0.1:8081/');

    The above code is sufficient to set up an HTTP server that listens on the local machine over 8081 port.

    Step 3: Create a hello.js file using the following command:

    $ nano hello.js

    Step 4: We will combine both the steps in a file hello.js and start our http server:

    Close the terminal by saving the file with CTRL + O and then exit it with CTRL + X.

    Step 5: Execute the hello.js to start the server with the following command:

    $ node hello.js

    Step 6: Check the server at http://127.0.0.1:8081/

    Finally, we have our first http server up.

    Uninstall Node.js on macOS

    Earlier we have discussed the installation of Node.js on Mac with three different methods. Now, we will discuss the different ways to uninstall Node.js from your system:

    1. Manually

    You will probably have to manually remove the executable node and other tools if you have installed node either by source or binary distribution. This is not simple, unfortunately, because several folders, such as npm and node modules, contain node resources.

    Use this official command to delete the node, node_modules folder:

    $ curl -ksO  
    https://gist.githubusercontent.com/nicerobot/2697848/raw/uninstall-node.sh
    $ chmod +x ./uninstall-node.sh
    $ ./uninstall-node.sh
    $ rm uninstall-node.sh

    2. Homebrew

    The Homebrew method is one of the easiest ways of installing and uninstalling node. If you're using the brew install node, just use the following command:

    $ brew uninstall node

    3. Node Version Manager(NVM)

    The Node Version Manager (NVM) is almost as simple to use as Homebrew. You can install several node versions on your system to allow you to migrate easily from one version to the next.

    Finally, when you're done, you'll probably want to get rid of one of the versions. You can do this quickly:

    $ nvm uninstall <version>

    For example:

    $ nvm uninstall v16.0.0.1

    Looking to unlock the power of coding? Dive into the world of Python with our unique course syllabus. Discover the endless possibilities and create your own digital masterpieces. Join us today and unleash your coding potential!

    Conclusion

    You have installed Node.js, npm successfully and checked the setup using a simple program. You can now use it to build applications on the client or on the server.

    You have also seen how to install Node.js via homebrew, probably the most popular macOS package manager.

    However, Node Version Manager is the fastest way to install Node.js. This provides additional control and versatility in adopting various versions of Node.js, which may be needed if you switch between different projects based on your needs.

    Profile

    Bala Krishna Ragala

    Blog Author

    Bala Krishna Ragala, Head of Engineering at upGrad, is a seasoned writer and captivating storyteller. With a background in EdTech, E-commerce, and LXP, he excels in building B2C and B2B products at scale. With over 15 years of experience in the industry, Bala has held key roles as CTO/Co-Founder at O2Labs and Head of Business (Web Technologies) at Zeolearn LLC. His passion for learning, sharing, and teaching is evident through his extensive training and mentoring endeavors, where he has delivered over 80 online and 50+ onsite trainings. Bala's strengths as a trainer lie in his extensive knowledge of software applications, excellent communication skills, and engaging presentation style.

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Web Development Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon