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

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.

October 1, 2019 โ€ข Frank Valcarcel

PyBay 2019: Emilyโ€™s Path to Becoming a Python Core Developer

From tackling impostor syndrome to mentoring future contributors, Emily’s story on becoming a Python Core Developer is a compelling tale of perseverance, community, and passion.