码迷,mamicode.com
首页 > Web开发 > 详细

How to Install Node.js and NPM on Mac OS

时间:2020-07-13 18:35:56      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:reason   ima   ofo   out   asics   evel   nta   nbsp   min   

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 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.

How to Install Node.js and NPM on Mac OS

标签:reason   ima   ofo   out   asics   evel   nta   nbsp   min   

原文地址:https://www.cnblogs.com/mouseleo/p/13294466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!