Back to homepage

Terminal - the emulator

Published on 13/07/2021
Got a better understanding of Terminal on macOS, NVM and NPM

I installed Node and NPM using NVM on macOS but npm command is not available in new Terminal windows. Why is that?

What is terminal?

Terminal is the terminal emulator included in macOS system. But terminal is not quite a correct term when speaking with developers. I would probably be asked about which terminal I use. The macOS Terminal? Wrong answer!

Windows - CMD, PowerShell …

I'm mainly a Windows user so I understand on Windows I can use CMD or PowerShell for command lines and they work differently. I can switch to terminals by running cmdor powershell.

> cmd
Microsoft Windows [Version 10.0.22000.71]
(c) Microsoft Corporation. All rights reserved.

> powershell
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

I didn't know that applies to macOS also. I mean, on macOS, actually also on Windows, there're many choices of terminals. For example, bash. Or zsh, which is the default terminal on latest macOS.

macOS - zsh …

Funny thing is mentioned in the Stencil installation troubleshoot guide - MacOS comes with zsh, however, its profile file is yet to be created so nothing can carry configurations at the moment.

Terminal profile files

For zsh, the profile file is .zshrc in ~/. To create it, you can simply run

touch ~/.zshrc  # create the file
source ~/.zshrc # load the profile file

Now you can install NPM by NVM and you'll be told to add something like this to the profile file

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

Presumably I had VS Code installed already, to edit the profile file, I can just run

code ~/.zshrc

Then copy paste the code block in the file.

How those are picked up?

Terminal emulator actually runs zsh and zsh reads the profile file to load NVM_DIR and other stuff. So now in a new Terminal window I can run npm without problem.

Be the first
to get my latest blog
Comments