Node.js is a powerful JavaScript runtime built on Chrome’s V8 JavaScript engine, commonly used for developing server-side and full-stack applications. Managing different Node.js versions on your system can be challenging, especially when working on multiple projects with varied requirements. That’s where Node Version Manager (NVM) comes in handy, allowing you to switch between Node.js versions effortlessly.
In this blog post, we will guide you through the step-by-step process of installing Node.js using NVM on a macOS M1 device. The M1 chip introduces some unique considerations, but with the right approach, you’ll have your development environment set up in no time.
Prerequisites
Before proceeding, ensure you have the following:
- A macOS M1 device.
- Terminal access with administrative privileges.
- Basic knowledge of using the command line.
Step 1: Install Homebrew
Homebrew is a popular package manager for macOS, simplifying the installation of software packages. If Homebrew is not already installed, follow these steps:
Open the Terminal application.
Run the following command to install Homebrew:
/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)“After installation, add Homebrew to your PATH by appending the following lines to your
~/.zshrc
file:echo ‘eval $(/opt/homebrew/bin/brew shellenv)’ >> ~/.zshrcApply the changes:
source ~/.zshrcVerify the installation by running:
brew –version
Step 2: Install NVM
Once Homebrew is installed, use it to install NVM:
- Install NVM using Homebrew:
brew install nvm
- Create a directory for NVM in your home directory:
mkdir ~/.nvm
- Configure your shell to use NVM by adding the following lines to your
~/.zshrc
file:export NVM_DIR="$HOME/.nvm" [ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \ . "/opt/homebrew/opt/nvm/nvm.sh" [ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \ . "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm"
- Apply the changes:
source ~/.zshrc
- Verify the NVM installation:
nvm --version
Step 3: Install Node.js Using NVM
With NVM set up, you can install and manage multiple versions of Node.js easily.
- List available Node.js versions:
nvm ls-remote
This will display all available versions of Node.js. Choose the version you need or the latest stable version. - Install a specific Node.js version:
nvm install <version_number>
For example, to install the latest LTS (Long-Term Support) version:nvm install --lts
- Verify the installation:
node --version
This should display the version of Node.js you just installed. - Set a default Node.js version:
nvm alias default <version_number>
Replace<version_number>
with the version you installed, or use--lts
to set the latest LTS version as default. - Switch between Node.js versions as needed:
nvm use <version_number>
Step 4: Test Your Node.js Installation
After installing Node.js, it’s a good idea to test your setup:
- Create a new directory for your test project:
mkdir ~/nodejs-test cd ~/nodejs-test
- Initialize a new Node.js project:
npm init -y
- Install a sample package, such as
express
:npm install express
- Create a simple JavaScript file,
app.js
:echo "const express = require('express'); const app = express(); const PORT = 3000; app.get('/', (req, res) => res.send('Hello, Node.js!')); app.listen(PORT, () => console.log('Server is running on http://localhost:' + PORT));" > app.js
- Run your application:
node app.js
- Open a web browser and navigate to
http://localhost:3000
. You should see the message “Hello, Node.js!”
Step 5: Troubleshooting
If you encounter issues, consider the following tips:
- NVM Command Not Found: Ensure you’ve properly configured your
~/.zshrc
file and sourced it. - Apple Silicon Compatibility: Use the terminal in Rosetta mode for older Node.js versions if necessary. However, most recent Node.js versions are compatible with M1 natively.
- Permission Issues: Avoid using
sudo
with NVM; it’s designed to manage permissions without superuser rights.
Conclusion
Installing Node.js using NVM on macOS M1 is a straightforward process, providing you with a flexible environment to manage multiple Node.js versions. This setup is particularly useful for developers working on diverse projects, as it allows seamless switching between versions. By following this guide, you’ll have your development environment up and running, ready to build and test Node.js applications efficiently.
Happy coding!