1. 程式人生 > 實用技巧 >How to Install Node.js and NPM on Mac OS

How to Install Node.js and NPM on Mac OS

Jun 29, 2018 -16 Comments

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 toinstall Homebrew on the Macfirst 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 likethe python instant web serverexcept 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 ininstilling the updated Python 3 on a Mactoo.

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.