Skip to Main Content
TWIL
bash
Node.js
Graphic representation of Emily showcasing a Shell script for Automatically Running nvm use, symbolizing the fusion of convenience and efficiency in managing Node versions in different project directories.

Welcome back to another edition of TWIL, our weekly series where we serve up bite-sized pieces of wisdom to spice up your tech know-how. This week, join Emily as she introduces a nifty Shell script to Automatically Run nvm use, a smart way to ensure the right Node version with a helpful nvm use command embedded in directory changesβ€”a true lifesaver for those seeking command line efficiency. Stay tuned as Emily demonstrates the ease of integrating this tweak into your workflow. With her tips, managing Node environments becomes as simple as hopping into a directory, perfect for the forgetful among us keen on keeping our Shell game strong.

Automatically Run nvm use Shell Function

Recently I've found myself forgetting to run nvm use , so I decided to add it to my usual cl function.

Here's the basic version:

# A function used as a more advanced version of `cd`
# - Changes into the specified directory
# - If an .nvmrc file exists, active it using `nvm use`
function cl () {
  cd "$@"

  if test -f ".nvmrc"; then
    echo "πŸ”ŽπŸ”ŽπŸ”Ž .nvmrc found - activating Node environment\n"
    nvm use
  fi
}

And my full cl command:

# A function used as a more advanced version of `cd`
# - Changes into the specified directory
# - Clears the screen
# - If an .nvmrc file exists, active it using `nvm use`
# - Lists the current directory
function cl () {
  cd "$@" && clear
  
  if test -f ".nvmrc"; then
    echo "πŸ”ŽπŸ”ŽπŸ”Ž .nvmrc found - activating Node environment\n"
    nvm use
  fi

  ls -laAhF
}
  • Shell
  • Bash
  • Helpers
  • Node.js
Emily Morehouse's profile picture
Emily Morehouse

Cofounder, Director of Engineering

Related Posts

Digital pioneer journeying through technological quirks, reminiscent of vast waterfalls and rocky terrains
April 5, 2017 β€’ Kyle Misencik

Infinitely dispatching actions + React, Redux & React Router v4

While implementing JWT for an application, we ran into a quirk with React Router v4. Here, we hope to show how to implement protected client-side routes while avoiding some of the pitfalls we encountered.

Image for 'This Week I Learned' blog post featuring tips on managing Git branches including renaming and syncing with remote changes.
October 8, 2019 β€’ Frank Valcarcel

TWIL 2019-10-04

Join us for ‘TWIL,’ where Emily shares innovative Git tips for managing branches with rewritten history. Learn to sync local branches with remote changes and rename them effectively.