How to install node js on mac
How to install node js on mac
Installing Node.js® and NPM on Mac
What’s Node.js® and NPM?
Node.js® is a JavaScript-based environment which you can use to create web-servers and networked applications. You can also use it to perform helpful tasks on your computer such as concatenating and minifying JavaScript files and compiling Sass files into CSS.
NPM is a “package manager” that makes installing Node “packages” fast and easy. A package, also called a module, is just a code library that extends Node by adding useful features. For example, the “request” module simplifies the process of making HTTP requests so you can easily get web resources from other sites.
NPM is installed when you install Node.js®
Although there is a Mac installer program on the Node website, we recommend using Homebrew to install and update Node.
Prerequisites
Installation Overview
Installing Node.js® and NPM is pretty straightforward using Homebrew. Homebrew handles downloading, unpacking and installing Node and NPM on your system. The whole process (after you have Homebrew installed) should only take you a few minutes.
Installation Steps
Test it!
Make sure you have Node and NPM installed by running simple commands to see what version of each is installed:
How to Update Node and NPM
New versions of Node and NPM come out frequently. You can use Homebrew to update the software it installs.
How to Uninstall Node and NPM
You can use Homebrew to uninstall packages that it installed:
How to Install Node.js and NPM on Mac
Home » Web Servers » How to Install Node.js and NPM on Mac
NPM is a package manager for JavaScript and the default package manager for Node.js. As such, NPM is installed alongside Node.js as a recommended feature.
This tutorial will show you how to install Node.js and NPM on Mac using the Homebrew package manager.
Note: Node.js has a PKG installer for macOS available on its website. However, installing with Homebrew is the recommended approach.
Install Homebrew
1. To install Homebrew on macOS, browse to Homebrew’s official website and copy the link displayed on the page.
Note: Do not copy the command from the screenshot since it may change in the future. Always visit the website for the up-to-date command.
2. Open the terminal and paste the command. Type the sudo password when prompted. Lastly, confirm that you want to install Homebrew by pressing Return.
Install Node.js and NPM on Mac
Note: For other operating systems, refer to our tutorials on installing Node.js and NPM on Windows, Ubuntu, and CentOS.
1. Type the following command to install Node.js and NPM:
Homebrew downloads and installs the dependencies.
2. When the installation finishes, confirm that you successfully installed Node.js by checking its version:
The system displays the Node.js version as the output of the command.
3. Next, check the installed version of NPM by typing:
The NPM version number is displayed.
Update Node.js and NPM on Mac
1. Before you proceed with upgrading the Node.js installation, update Homebrew’s repositories by typing:
2. When the update completes, type the following command:
The output in the example shows that the newest version of Node.js is already installed on the system.
Note: For additional details, check our article How to Update Node.js and NPM.
Uninstall Node.js and NPM on Mac
1. Uninstall both Node.js and NPM with the brew uninstall command:
2. Remove all the unused dependencies with:
This command removes the packages that brew uninstall did not remove.
How to Install Node.js and NPM on Mac OS
Node JS is the popular Javascript runtime environment that is widely used by many developers, and npm is the accompany package manager for the Node.js environment and Javascript. When you install Node.js, you will find npm is installed as well, thus if you want npm you need to install NodeJS.
There are several ways to install Node.js and NPM on the Mac, including using a prebuilt packaged installer, or by using Homebrew. This tutorial will cover both, and either approach should work find on any modern version of MacOS system software.
How to Install Node.js and npm on Mac OS with Homebrew
The easiest way to install node.js and npm is with the Homebrew package manager, which means first you will need to install Homebrew on the Mac first if you have not done so already. It’s always a good idea to update Homebrew before installing a Homebrew package, so run the following command to do that:
Assuming you already have Homebrew on the Mac, then you can run the following command into the Terminal application to install both Node.js and npm:
brew install node
Installing NodeJS / NPM via Homebrew is arguably easier than using any other method, and it also makes it simple to keep node.js and npm updated. It also has the added benefit of making it relatively simple to uninstall down the road if you decide you no longer need it.
Installing Node.js & NPM on the Mac with a package installer
If you don’t want to use Homebrew for whatever reason, the other next easiest option is to use a the prebuilt installer from nodejs.org:
You can run the installer like any other installation package on the Mac.
How to Check if NPM and Node.js are installed on a Mac
How to Test that Node.js is Working
Once the node.js package is installed on the Mac you can test it’s working by starting a simple web server. Create a file named “app.js” that contains the following code syntax:
const hostname = ‘127.0.0.1’;
const port = 3000;
const server = http.createServer((req, res) => <
res.statusCode = 200;
res.setHeader(‘Content-Type’, ‘text/plain’);
res.end(‘Hello from NodeJS\n’);
>);
Save that app.js file to the current directory, then you can start the web server with the following command:
Then launch a web browser (your default or otherwise) and go to the following URL:
You should see a message stating “Hello from Node.js”.
That simple node.js web server is sort of like the python instant web server except of course it’s using node rather than python. Speaking of Python, if you’re installing Node.js and NPM you may also be interested in instilling the updated Python 3 on a Mac too.
You can also install and use Grunt CLI task runner to test node and npm, which can be installed through npm:
You can then run ‘grunt’ from the command line.
That should just about cover the basics of installing NodeJS and npm on a Mac. If you have any other tips, tricks, suggestions, or advice, feel free to share them in the comments below.
Related
Enjoy this tip? Subscribe to our newsletter!
Get more of our great Apple tips, tricks, and important news delivered to your inbox with the OSXDaily newsletter.
Thank you!
You have successfully joined our subscriber list.
Related articles:
17 Comments
The article provides detailed information about node.js and npm installation on macOS and I especially liked that you provide all commands. But I want about that what does npm stands for. My thanks and appreciation to you.
//Use this for app.js.
//Not sure why the smart quotes are used above.
const http = require(‘http’);
const hostname = ‘127.0.0.1’;
const port = 3000;
const server = http.createServer((req, res) => <
res.statusCode = 200;
res.setHeader(‘Content-Type’, ‘text/plain’);
res.end(‘Hello from NodeJS\n’);
>);
server.listen(port, hostname, () => <
console.log(‘Server running at http://$
>);
Here’s how to uninstall a package with NPM too:
npm uninstall NAME
hi, I’m at a loss. Need node and npm for a package we want to run. Macbook air, High Sierra 10.13.6. Installed command line, homebrew, then node and npm using brew. Got good responses from version test:
But get syntax errors when I try to run the app.js – I did write it in brackets and the code looks clean (I copied and pasted) but I’m getting this:
tigger$ node app.js
/Users/tigger/app.js:1
(function (exports, require, module, __filename, __dirname) < const http = require(‘http’);
^
SyntaxError: Invalid or unexpected token
at new Script (vm.js:74:7)
at createScript (vm.js:246:10)
at Object.runInThisContext (vm.js:298:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:266:19)
Suggestions? Is there something I missed? Please help! Thanks
Looks like a javascript error, it’s probably nothing to do with node but instead something with the Javascript itself, thus check the actual source itself for syntax issues or even formatting/character problems.
If the file is not a plain text document for example, or sometimes copying/pasting from the web, or if you’re on a Mac sometimes the quotations get changed or some characters can be ‘autocorrected’ to different quotation marks etc especially if you used a regular text editor rather than a code editor (plain text always!)
You likely need to get the source for “app.js” again, try to download it from git or somewhere similar to make sure it does not get mangled.
YES! Actually, when I look at the code in this post, the single quotes appear to be smart quotes. So when I copy/pasted and saved as plain text, they still were smart quotes. I simply hand replaced them all with regular single quotes, and it ran like a charm! Thank you for the response, so I took a closer look. (I was using a code editor, Brackets, but it did not point out the issue clearly to me)
@moss and @tig: Thank you for your sharp eagle’s eye. I likewise encountered the script error when I tried to launch the server. Following tig’s suggestion, the server installed and ran just fine after fixing the fancy quotes. 🙂
Thanks for the article, Peter. You’re responses to the comments were my favorite part though!
So now I’ve installed this what can I do with it?
(Except I haven’t installed it because I don’t know what to do with it!)
I am always puzzled by internet comments. It’s as if people read the headline (or not even that) and then jump right to the comments to rant, rave, or opine about what they could have learned had they read the article they commented on.
Stunningly, the very first sentence of this article answers the question posed here by Steve:
“Node JS is the popular Javascript runtime environment that is widely used by many developers, and npm is the accompany package manager for the Node.js environment and Javascript”
Sometimes I think there should be a quiz before allowing internet comments!
How to Install Node.JS on a Mac
Read it in 7 Mins
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:
Software Requirements:
Installation Procedure
In this article, we are going to look at three different ways to install Node.js on macOS.
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.
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:
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:
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:
Step 3: Verify the installation and check the version of Node.js and npm.
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:
/.bashrc. Depending on what you have on your machine you can use curl
Use the following command:
Step 2: Add the source lines in your shell profile. You can use
/.bashrc. In this article, we will be using zsh shell:
Step 3: Paste the following lines of code:
Close the file by using esc + w + q
Step 4: Use the following command to reload the shell:
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.
Note: You can check the various commands of nvm with the following command:
Step 6: Install LTS version:
Step 7: Install the latest version of 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:
Step 9: Install 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:
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:
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!
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:
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:
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:
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:
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:
LearnInstall Node.js and NPM on a Mac: How-To Guide
Dave McFarland
writes on October 8, 2014
JavaScript is one of the most popular programming languages in the world. Because it’s built into most web browsers, programmers and web designers can use JavaScript to add interactive features to websites that reach billions of people. But in the past couple of years, JavaScript has started to play a larger role outside of the browser, due in large part to Node.js.
Node.js is a tool for building fast network applications. It’s known as a “JavaScript runtime environment” which simply means it lets you write JavaScript code that can run on your computer free of any web browser. Node.js is used to create fast web servers by companies like Walmart, eBay and Netflix.
But before you can use Node.js or NPM you need to install them — while the NodeJS website includes an installer, there’s a better way to install them on a Mac. In this article, I’ll take you through the process of installing Node.js and NPM on a Mac using Homebrew. In another article, I’ll show you Windows users how to install them on the Windows operating system.
Prerequisites
Before you install Node.js and NPM you’ll first need to have some familiarity with the Mac Terminal application. Terminal lets you dig into the underbelly of the operating system and issue text commands to your computer. You’ll need to use Terminal (or a similar application like iTerm) to not only install Node.js but also to use it and NPM.
Before you can install Node, you’ll need to install two other applications. Fortunately, once you’ve got these on your machine, installing Node takes just a few minutes.
Are you ready to start learning?
Learning with Treehouse for only 30 minutes a day can teach you the skills needed to land the job that you’ve been dreaming about.
Why Homebrew?
Observant readers will notice in the screenshot above that there’s an installer for NodeJS. You can download it directly from NodeJS.org. I recommend Homebrew over that installer for a few reasons:
Installation
Installing Node.js and NPM is pretty straightforward using Homebrew. Homebrew handles downloading, unpacking and installing Node and NPM on your system. The whole process (after you have XCode and Homebrew installed) should only take you a few minutes.
To make sure you have Node and NPM installed, run two simple commands to see what version of each is installed:
Check and Update Your Node and NPM Versions with Homebrew
New versions of Node and NPM come out frequently. You can use Homebrew to update the software it installs.
How to Uninstall Node and NPM
You can use Homebrew to uninstall packages that it installed: brew uninstall node
With Node.js and NPM installed you’ll soon be able to take advantage of the huge world of NPM modules that can help with a wide variety of tasks both on the web server and on your desktop (or laptop) machine. The NPM site lists all of the official Node packages making it easy to make the choice. Have fun and check out my current courses at Treehouse.
Learn with Treehouse
What sets Treehouse apart is their dedication to helping you find your perfect job or develop your own business. – SwitchUp.org
Follow us on Twitter, Instagram, and Facebook for our favorite tips, and to share how your learning is going. We’ll see you there!
If you liked reading this article, you should also look at these two:
GET STARTED NOW
Learning with Treehouse for only 30 minutes a day can teach you the skills needed to land the job that you’ve been dreaming about.