Node.js runs on numerous platforms (Home windows, Linux, Unix, Mac OS, and many others.) On this tutorial, we’ll talk about about Node.js set up on macOS.
Npm is the platform for Node.js bundle administration. It affords a software for Node.js libraries to be put in, and their variations and dependencies managed.
- RAM: four GB
- Storage: 256 GB of Onerous Disk House
Software program Necessities:
- Internet Browser: Any browser akin to Google Chrome, Mozilla Firefox, Microsoft Edge, Safari.
- XCode: XCode software program is utilized by Apple in constructing Mac and iOS functions, so it offers the devices you have to compile Mac software program. You will discover XCode within the Apple App Retailer.
- Homebrew: Homebrew is a bundle supervisor for the Mac. It readily permits the set up of most open-source software program (like Node). On the Homebrew web site, you will discover out extra about Homebrew.
- Working System: macOS
Set up Process
On this article, we’re going to have a look at three alternative ways to put in Node.js on macOS.
- utilizing the macOS installer
- utilizing homebrew
- utilizing Node Model Supervisor
So, allow us to begin.
1. Utilizing the macOS installer
Step 1: Go to the Node.js web site to replace your Mac platform with the constructed installer.
Node.js updates comprise two sorts, long-term help (LTS) and new releases. LTS variations are refined and bug-free, and are ample for many day by day customers. Present variations (Newest LTS 14.16.1) are extra experimental and embrace the brand new performance, which can’t be accomplished or crashed occasionally. By highlighting the sector on the primary tab, you’ll be able to transfer between LTS and present variations. Once more, most customers use the LTS model. Due to this fact, you’ll be able to simply click on on macOS installer, which can obtain the Node.js installer, if the LTS tab is highlighted in darkish inexperienced.
Step 2: Obtain .pkg installer and open the downloaded file with default installer.
Step three: After working the .pkg installer, comply with the directions on the interface.
1. Introduction window: choose Proceed
2. Licence window: Choose Proceed and a pop-up window will ask you to agree or disagree. Click on on Comply with proceed.
three. Choose Set up and Authenticate your macOS password.
four. You will note that Node.js and npm are put in on the interface.
Step four: Confirm the set up of Node.js and npm by utilizing the next instructions on the terminal:
node -v npm -v
Utilizing homebrew to put in and replace Node.js
Although there are a variety of options within the command line interface of macOS, Linux and different Unix methods do not need a good bundle supervisor. A Bundle Supervisor consists of a sequence of software program instruments that robotically set up, configure and replace the software program.
They handle the software program in a central location and keep all of the gadget software program packages in broadly used codecs. The Homebrew software program bundle administration framework is free and open-source and simplifies the set up of macOS software program. The newest model of Node.js will be put in utilizing Homebrew.
Step 1: Use the next command to put in Homebrew:
s$ /usr/bin/ruby -e "$(curl -fsSL https://uncooked.githubusercontent.com/Homebrew/set up/grasp/set up)"
When CURL downloads the script, the Ruby-interpreter begins the Homebrew set up course of, which ships with macOS.
Throughout the operation, you may be requested to enter your password. Whilst you do not see them, the machine information your keystrokes, so as soon as your password has been entered, click on the RETURN key.
Step 2: As soon as homebrew is put in, use the next command to put in Node.js:
$ brew replace $ brew set up node
Step three: Confirm the set up and test the model of Node.js and npm.
$ node -v $ npm -v
Homebrew all the time installs solely the most recent model of Node.js. This can be problematic since a sure model of Node.js is perhaps required to operate in functions. It may be a very good factor to have the liberty to make use of these variations. The simplest strategy to resolve this drawback is by utilizing NVM, the Node Model Supervisor.
Utilizing nvm to put in and replace Node.js
NVM is a bash script for a number of lively variations of Node.js. Comply with these steps to put in Node.js:
Step 1: The script copies the nvm-repository to ~/.nvm after which attaches the sources to the profile of the shell: ~/.bash profile, ~/.zshrc and ~/.profile or ~/.bashrc. Relying on what you have got in your machine you should use curl
Use the next command:
$ curl -o- https://uncooked.githubusercontent.com/nvm-sh/nvm/v0.35.three/set up.sh | bash
Step 2: Add the supply traces in your shell profile. You need to use ~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc. On this article, we will likely be utilizing zsh shell:
$ vim .zshrc
Step three: Paste the next traces 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"
Shut the file by utilizing esc + w + q
Step four: Use the next command to reload the shell:
$ supply ~/.zshrc
Step 5: You possibly can confirm the set up with the next command and if the display screen proven had nvm written because the output, then the set up is profitable.
$ command -v nvm
Notice: You possibly can test the varied instructions of nvm with the next command:
$ nvm + tab key
Step 6: Set up LTS model:
$ nvm set up –lts
Step 7: Set up the most recent model of node:
$ nvm set up node
That is an alias for the most recent model.
Step eight: You possibly can record out all of the put in Node.js variations accessible in your system:
$ nvm record
Step 9: Set up NPM:
$ nvm install-latest -npm
Step 10: This installs the most recent NPM. After you’ve got arrange NPM, you’ll be able to check out various helpful instructions:
Listing and replace globally put in packages:
$ npm ls -g --depth=zero $ npm replace -g
Create a easy Program
Let’s create a easy program “Hi there, world.” This ensures that the environment works and that you would be able to construct and run a Node.js program conveniently.
Step 1: With a purpose to load the http module and retailer the returned HTTP occasion in a http variable, we use the directive:
var http = require("http");
Step 2: To construct a server occasion, use the created http occasion and name http.createServer() and join it to port 8081 via the servers occasion. Go a operate with request and response parameters. We’ll print Hi there World!
http.createServer(operate (request, response) // Ship the HTTP header // HTTP Standing: 200 : OK // Content material Kind: textual content/plain response.writeHead(200, 'Content material-Kind': 'textual content/plain'); response.finish('Hi there Worldn'); ).hear(8081); console.log('Server working at http://127.zero.zero.1:8081/');
The above code is ample to arrange an HTTP server that listens on the native machine over 8081 port.
Step three: Create a hi there.js file utilizing the next command:
$ nano hi there.js
Step four: We’ll mix each the steps in a file hi there.js and begin our http server:
Shut the terminal by saving the file with CTRL + O after which exit it with CTRL + X.
Step 5: Execute the hi there.js to start out the server with the next command:
$ node hi there.js
Step 6: Examine the server at http://127.zero.zero.1:8081/
Lastly, we have now our first http server up.
Uninstall Node.js on macOS
Earlier we have now mentioned the set up of Node.js on Mac with three totally different strategies. Now, we’ll talk about the alternative ways to uninstall Node.js out of your system:
You’ll in all probability should manually take away the executable node and different instruments when you’ve got put in node both by supply or binary distribution. This isn’t easy, sadly, as a result of a number of folders, akin to npm and node modules, include node sources.
Use this official command to delete the node, node_modules folder:
$ curl -ksO https://gist.githubusercontent.com/nicerobot/2697848/uncooked/uninstall-node.sh $ chmod +x ./uninstall-node.sh $ ./uninstall-node.sh $ rm uninstall-node.sh
The Homebrew technique is among the best methods of putting in and uninstalling node. When you’re utilizing the brew set up node, simply use the next command:
$ brew uninstall node
three. Node Model Supervisor(NVM)
The Node Model Supervisor (NVM) is nearly as easy to make use of as Homebrew. You possibly can set up a number of node variations in your system to can help you migrate simply from one model to the following.
Lastly, while you’re completed, you will in all probability need to do away with one of many variations. You are able to do this shortly:
$ nvm uninstall <model>
$ nvm uninstall v16.zero.zero.1
You’ve put in Node.js, npm efficiently and checked the setup utilizing a easy program. Now you can use it to construct functions on the consumer or on the server.
You’ve additionally seen learn how to set up Node.js through homebrew, in all probability the most well-liked macOS bundle supervisor.
Nonetheless, Node Model Supervisor is the quickest strategy to set up Node.js. This offers further management and flexibility in adopting numerous variations of Node.js, which can be wanted when you change between totally different initiatives based mostly in your wants.