Using NVM as a Command Instead of Sourcing It Every Time

Say goodbye to those ***** `module.exports` statements and hello to NVM as a command instead of sourcing it every time!

You might be wondering, “Why would I want to do this?” Well, let me tell you there are several benefits to using NVM as a command:

1. It’s faster than sourcing your `.bashrc` or `.zshrc` file every time you open a new terminal window (or tab). This is because the commands are executed in memory, rather than being read from disk each time.

2. It’s more convenient to use NVM as a command instead of having to remember which version of Node you want to use for each project. With just one command, you can switch between different versions without any hassle!

3. It’s less error-prone than sourcing your `.bashrc` or `.zshrc` file every time. This is because NVM handles version management for you, so there are fewer opportunities for mistakes to occur (like forgetting to add a semicolon at the end of a line).

So how do we use NVM as a command instead of sourcing it? It’s actually pretty simple! First, make sure that NVM is installed on your system. If you haven’t already done so, follow these instructions:

1. Install Node using your preferred method (e.g., via Homebrew or Chocolatey).

2. Add the following line to your `.bashrc` or `.zshrc` file:

# Set the NVM directory to the user's home directory
NVM_DIR="$HOME/.nvm"

# Check if the nvm.sh file exists in the NVM directory and if it does, load it
if [ -s "$NVM_DIR/nvm.sh" ]; then
  # The backslash (\) allows the command to continue on the next line
  # Load the nvm.sh file, which sets up the nvm environment
  \. "$NVM_DIR/nvm.sh"
fi

3. Save the changes and exit your terminal window (or tab).

4. Open a new terminal window or tab, and run `source ~/.bashrc` or `source ~/.zshrc`.

5. Verify that NVM is installed by running `nvm –version`. You should see something like this:


# This code segment checks the version of NVM installed on the system
# and prints it to the console.
nvm --version

# This code segment is the expected output of the previous command,
# indicating the version of NVM installed.
0.39.1

Now, let’s say you have a project that requires Node version 8.x. To switch to that version using NVM as a command instead of sourcing it every time, simply run `nvm use v8`. This will set the current Node version to 8.x for this terminal session (or tab).

That’s it! You can now continue working on your project without having to worry about which version of Node you’re using. And if you ever need to switch back to a different version, just run `nvm use ` again. It’s that easy!

Give it a try and let us know what you think in the comments below!

SICORPS