DEV Community

Cover image for NVM Basics & Bash Aliases: Simplify Your Development Workflow
Diona Rodrigues
Diona Rodrigues

Posted on • Originally published at dionarodrigues.dev

NVM Basics & Bash Aliases: Simplify Your Development Workflow

As developers, managing different versions of Node.js and streamlining terminal commands are essential for productivity. Tools like Node Version Manager (NVM) and Bash Aliases can make a big difference, especially when working on multiple projects with different dependencies, like varying Node or React versions.

In this article, we’ll explore how NVM helps you manage Node.js versions for different projects and how Bash Aliases can simplify repetitive commands, making your development workflow faster and more efficient. Let’s dive in!

NVM (Node Version Manager)

NVM (Node Version Manager) is a crucial tool for managing multiple versions of Node.js, especially when working on projects that require different Node or React versions. For instance, if one project needs an older React version while another uses a newer one, NVM lets you easily switch between versions to maintain compatibility without interfering with other projects. This flexibility ensures that each project runs with the right version of Node.js, eliminating version conflicts and improving your workflow.

How to install NVM

You can install NVM first using a script or package manager (like curl or wget). Open your terminal and run the following command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
Enter fullscreen mode Exit fullscreen mode

This will download and install NVM. After installation, close and reopen your terminal.

Install Node.js via NVM

After installing NVM, you can use it to install different versions of Node.js with commands like nvm install <version>:

nvm install 22
Enter fullscreen mode Exit fullscreen mode

Basic NVM commands

Use a specific Node.js Version

nvm use 22
Enter fullscreen mode Exit fullscreen mode

List all installed Node.js versions

nvm ls
Enter fullscreen mode Exit fullscreen mode

Set a default Node.js version:

nvm alias default 22
Enter fullscreen mode Exit fullscreen mode

With these commands, you can manage and switch between different versions of Node.js for various projects. Learn more in the NVM Github repository.

Bash Aliases

Bash Aliases are shorthand commands that allow you to simplify long or complex commands. By creating aliases, you can save time and effort when running frequently used commands, whether it's for navigating to project directories or executing npm scripts.

Creating Bash Aliases

To create custom aliases, you'll need to modify your Bash configuration file. Here's how to do it:

Edit the Bash Configuration File

Open your ~/.bashrc file using your preferred text editor (we’ll use nano here):

nano ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Add Aliases

In the .bashrc file, add the following alias syntax to define your custom commands.

alias <alias_name>="<command>"
Enter fullscreen mode Exit fullscreen mode

Here's an example of Bash aliases that you can use to switch between different Node versions for different React project paths.

# React Project 1 (Requires Node 16.x)
alias react_project_1="cd ~/Documents/develop/project-1 && nvm use 16 && npm start"

# React Project 2 (Requires Node 20.x)
alias react_project_2="cd ~/Documents/develop/project-2 && nvm use 20 && npm start"
Enter fullscreen mode Exit fullscreen mode

So, the aliases above, mean:

Change the current directory to the project folder, switch to Node.js version 16 (or 20, depending on the alias), and start the React development server.

Reload the Bash File

To activate the aliases, after saving the file, you'll need to reload the .bashrc file by running:

source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Using the Bash aliases

Use the aliases just like a regular command:

react_project_1
Enter fullscreen mode Exit fullscreen mode

Conclusion

By utilizing NVM and Bash Aliases, you can significantly simplify your development workflow. NVM allows you to manage different versions of Node.js for various projects, while Bash Aliases save you time by turning long commands into easy-to-remember shortcuts.

Together, these tools can help you focus more on development and less on managing environments or repeatedly typing out long commands.

For further reading, check out these helpful resources:

By implementing these tools, you'll be able to optimize your development environment and work more efficiently. Happy coding! 😊

Top comments (0)