How to Install Node.js Using NVM on macOS M1

5/5 - (1 vote)

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:

  1. A macOS M1 device.
  2. Terminal access with administrative privileges.
  3. 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:

  1. Open the Terminal application.

  2. Run the following command to install Homebrew:

    /bin/bash -c $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)
  3. After installation, add Homebrew to your PATH by appending the following lines to your ~/.zshrc file:

    echo ‘eval $(/opt/homebrew/bin/brew shellenv)’ >> ~/.zshrc
  4. Apply the changes:

    source ~/.zshrc
  5. Verify the installation by running:

    brew –version

Step 2: Install NVM

Once Homebrew is installed, use it to install NVM:

  1. Install NVM using Homebrew:brew install nvm
  2. Create a directory for NVM in your home directory:mkdir ~/.nvm
  3. 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"
  4. Apply the changes:source ~/.zshrc
  5. 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.

  1. List available Node.js versions:nvm ls-remoteThis will display all available versions of Node.js. Choose the version you need or the latest stable version.
  2. Install a specific Node.js version:nvm install <version_number>For example, to install the latest LTS (Long-Term Support) version:nvm install --lts
  3. Verify the installation:node --versionThis should display the version of Node.js you just installed.
  4. 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.
  5. 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:

  1. Create a new directory for your test project:mkdir ~/nodejs-test cd ~/nodejs-test
  2. Initialize a new Node.js project:npm init -y
  3. Install a sample package, such as express:npm install express
  4. 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
  5. Run your application:node app.js
  6. 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!

Leave a Comment

5 × 5 =

CRICKET LIVE STREAMING