How to Install Node.js and NPM on Mac OS

Jun 29, 2018 - 17 Comments

How to install NodeJS 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:

brew update

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

After you have installed node.js with npm, you can confirm that the two are installed by issuing either command with a -v flag to check the version:

node -v

and

npm -v

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 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://${hostname}:${port}/`);
});

Save that app.js file to the current directory, then you can start the web server with the following command:

node app.js

Then launch a web browser (your default or otherwise) and go to the following URL:

http://localhost:3000

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:

npm install -g grunt-cli

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 articles:

Posted by: Paul Horowitz in Command Line, Mac OS, Tips & Tricks

17 Comments

» Comments RSS Feed

  1. jhanvi says:

    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.

  2. RD says:

    //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://${hostname}:${port}/’);
    });

  3. Violo says:

    Here’s how to uninstall a package with NPM too:

    npm uninstall NAME

    More

    https://docs.npmjs.com/cli/uninstall

  4. Tig says:

    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:

    Lothlorien:~ tigger$ node -v
    v10.7.0
    Lothlorien:~ tigger$ npm -v
    6.2.0

    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:

    Lothlorien:~ 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

    • moss says:

      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.

      • tig says:

        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)

      • ww777 says:

        @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. :)

  5. Martin says:

    Thanks for the article, Peter. You’re responses to the comments were my favorite part though!

  6. Steve Jackson says:

    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!)

    • Peter Ragan says:

      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!

      Now giving Steve benefit of the doubt here, perhaps he is unfamiliar with javascript, package managers, and software development in general. That’s actually fairly understandable as most people have no development background. Like myself, I have no experience with certain topics but usually if something is beyond my scope I just ignore it. But let’s assume someone wants to learn, thus why they comment, learning is great! So in that case this is for developers and programmers who are already familiar with node.js as a development platform, typically those who work with web apps. If you have no prior programming experience, it can be a cumbersome task to get started, but it would be wise to start learning the general concepts first with something like an Intro to Javascript book, or something aimed at programmers for total beginners. A lot of it is just going through the motions, like learning much else, it takes a while to “stick” and it’s likely you will follow some guides to learn, have something “working” as outlined in the book, but not totally understand why, that’s just part of the learning process. Sort of like learning discrete math, or a foreign language, or a musical instrument, it’s just a matter of commitment and practice.

      As for node.js this is a helpful resource: https://docs.npmjs.com/

  7. Max says:

    What does NPM stand for?

  8. Luke says:

    Couldn’t you just navigate to any website which uses it, get out your javascript inspector, and copy it from there?

    • Pablo says:

      No, what you describe would be to download a javascript file from a website, whereas this is a software development library and toolkit.

      node.js is a library, npm is a package manager and sort of nearly version control for sharing code snippets. This is pretty specific for web developers really, so most people won’t have a practical need for it.

      This can be helpful if you are interested in this stuff;

      https://www.npmjs.com/get-npm

      It is a good toolkit, very commonly used in programming, webdev and devops circles.

  9. larry says:

    It be nice it have a discussion about why to do this.

    • Peter Ragan says:

      larry, if you need nodejs and npm then you install node.js and npm. Simply, you do this if you need it. Almost exclusively for developers.

      https://docs.npmjs.com/

      If you don’t need node.js or npm, you would not do this. Simply, you don’t do this if you don’t need it. Which is almost exclusively everyone else.

      Like most other things in the universe, not everything is universally applicable.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site