Question

Fix “tsc: command not found” When Installing TypeScript on macOS

Question

I installed TypeScript globally with the following command:

npm install -g typescript

However, running the following command returns a tsc: command not found error:

tsc --version

How can I verify whether TypeScript was installed successfully, find where npm installed it, and configure macOS so that the tsc command is available? My environment is Unix/macOS El Capitan 10.11.6 with Node.js 4.4.3 and npm 3.10.5.

Short Answer

You will learn how the TypeScript compiler is installed through npm, why a successful global installation may still produce tsc: command not found, and how the shell PATH determines whether a command can run. You will also learn commands for checking npm’s installation locations and reliable alternatives such as npx and project-local TypeScript installations.

Concept

The tsc command is the TypeScript compiler command-line interface (CLI). It is included in the typescript npm package.

When you run:

npm install -g typescript

npm installs TypeScript into its global package directory. It also creates a runnable command named tsc in npm’s global bin directory.

Your terminal does not search every folder on your computer when you type a command. Instead, it searches only the directories listed in the PATH environment variable. Therefore, this error:

tsc: command not found

usually means one of these things:

  • TypeScript was not installed successfully.
  • npm created tsc, but npm’s global bin directory is missing from PATH.
  • You installed TypeScript under a different Node/npm installation than the one your shell currently uses.
  • The terminal session needs to be restarted after changing configuration.

This distinction matters because installing a package and making its executable discoverable are related, but separate, steps.

Mental Model

Think of npm’s global bin directory as a tool drawer and PATH as a list of drawers your terminal is allowed to open.

Installing TypeScript puts the tsc tool into npm’s drawer. When you type tsc, your shell checks each drawer listed in PATH.

  • If npm’s drawer is on the list, the shell finds and runs tsc.
  • If it is not on the list, the shell reports command not found even though the tool may already be installed.

The fix is not necessarily reinstalling TypeScript. Often, the fix is telling the shell where npm keeps globally installed commands.

Take Quiz

Syntax and Examples

The TypeScript compiler is installed with npm:

npm install -g typescript

Verify that npm considers TypeScript globally installed:

npm list -g --depth=0 typescript

A successful result includes a version, similar to:

/usr/local/lib
└── typescript@5.7.2

Find npm’s global executable directory:

npm bin -g

On older npm versions, this commonly prints a directory such as:

/usr/local/bin

Check whether the tsc executable exists there:

ls -l "$(npm bin -g)/tsc"

If it exists, try running it through its full path:

"$(npm bin -g)/tsc" --version

If this prints a TypeScript version, TypeScript is installed correctly. The remaining issue is that the global bin directory is not available through PATH.

To see where the shell currently finds tsc, use:

command -v tsc

If it prints no output, no matching executable was found in the current PATH.

Step by Step Execution

Consider this diagnostic sequence:

npm install -g typescript
npm list -g --depth=0 typescript
npm bin -g
ls -l "$(npm bin -g)/tsc"
"$(npm bin -g)/tsc" --version

What happens at each step:

  1. npm install -g typescript downloads and installs the global typescript package.
  2. npm list -g --depth=0 typescript asks npm whether that package exists in the global package list.
  3. npm bin -g prints the directory where npm places global executable commands.
  4. ls -l "$(npm bin -g)/tsc" checks whether the tsc command file was created in that directory.
  5. "$(npm bin -g)/tsc" --version runs that exact file directly, without relying on PATH.

For example, if npm bin -g prints /usr/local/bin, the final command is effectively:

/usr/local/bin/tsc --version

If the direct command works but this does not:

tsc --version

then the shell cannot search /usr/local/bin. Add the printed bin directory to PATH in your shell configuration, then open a new terminal:

export PATH="$(npm bin -g):$PATH"

For a persistent setup on older macOS systems using Bash, add the appropriate fixed directory to ~/.bash_profile, for example:

export PATH="/usr/local/bin:$PATH"

Then reload the file:

source ~/.bash_profile

Use the actual directory reported by npm bin -g; do not assume every npm installation uses /usr/local/bin.

Real World Use Cases

TypeScript compilation is commonly used in several workflows:

  • Frontend applications: Compile .ts and .tsx source files into JavaScript before browser deployment.
  • Node.js APIs: Type-check server code during development and build JavaScript for production.
  • CI pipelines: Run tsc --noEmit to catch type errors before merging a pull request.
  • Libraries: Generate JavaScript and declaration files (.d.ts) for package consumers.
  • Editor integration: Language tools use the project’s TypeScript version to provide diagnostics and autocomplete.

In all of these cases, the project needs a dependable way to locate the compiler. That is why project-local installation is usually preferred over relying on a developer’s global installation.

Take Quiz

Real Codebase Usage

In most production codebases, TypeScript is installed locally as a development dependency rather than globally:

npm install --save-dev typescript

The executable is then available in node_modules/.bin. npm automatically exposes that directory to scripts in package.json:

{
  "scripts": {
    "typecheck": "tsc --noEmit",
    "build": "tsc -p tsconfig.json"
  }
}

Run the scripts with:

npm run typecheck
npm run build

This pattern is valuable because every developer and CI server uses the version recorded in package.json and the lockfile.

You can also run a locally installed compiler directly with npx:

npx tsc --version
npx tsc --noEmit

For one-off execution, npx looks for the project-local executable first. This avoids many global PATH problems.

A typical project also stores compiler settings in tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2020",
    "strict": true,
    "outDir": "dist"
  },
  "include": ["src"]
}

Then npm run build compiles files consistently for everyone working on the codebase.

Common Mistakes

Assuming installation automatically updates PATH

This can fail even after a successful install:

npm install -g typescript
tsc --version

Installing creates the command, but your shell must be able to search its directory. Check it with:

npm bin -g
echo "$PATH"

Adding the global package directory instead of the bin directory

These are different locations:

npm root -g  # Global packages, such as .../lib/node_modules
npm bin -g   # Executable commands, such as .../bin

For tsc, add the result of npm bin -g to PATH, not the result of npm root -g.

Using sudo as the first solution

This is tempting:

sudo npm install -g typescript

It can create files owned by root, causing later permission problems. First inspect your Node and npm setup. Node version managers and project-local dependencies are usually safer approaches.

Editing the wrong shell startup file

Older macOS installations often use Bash and load ~/.bash_profile for login shells. Newer macOS installations commonly use Zsh and load ~/.zshrc.

Check your current shell:

echo "$SHELL"

After editing the correct file, open a new terminal or reload it with source.

Confusing tsc with ts-node

tsc compiles or type-checks TypeScript. It does not directly run a TypeScript program in the same way node runs JavaScript. ts-node is a separate package with a different purpose.

Depending only on a global compiler

A global compiler can differ across developers and CI machines. Prefer this project dependency:

npm install --save-dev typescript

Then use npm run build or npx tsc.

Comparisons

ApproachCommand exampleBest useMain consideration
Global TypeScript installationnpm install -g typescriptPersonal command-line useRequires npm global bin directory in PATH
Local project installationnpm install -D typescriptApplications, libraries, teams, CIVersion is controlled by the project
npm scriptnpm run buildRepeatable project tasksRequires a script in package.json
npx executionnpx tsc --versionRunning the local tool without a scriptUses the local dependency when available
Full executable path"$(npm bin -g)/tsc" --versionDiagnosing PATH issuesWorks, but is inconvenient for everyday use
CommandWhat it shows
npm list -g --depth=0 typescriptWhether TypeScript is globally installed
npm root -gWhere global npm packages are stored
npm bin -gWhere global executable commands are stored
command -v tscWhich tsc command the shell will run
echo "$PATH"Directories searched for commands
Take Quiz

Cheat Sheet

# Install TypeScript globally
npm install -g typescript

# Verify the global package installation
npm list -g --depth=0 typescript

# Print global package directory
npm root -g

# Print global executable directory
npm bin -g

# Check whether the global tsc file exists
ls -l "$(npm bin -g)/tsc"

# Run global tsc by exact path
"$(npm bin -g)/tsc" --version

# Check whether the shell can find tsc
command -v tsc

# Inspect command search directories
echo "$PATH"

# Add npm global commands to PATH for the current shell session
export PATH="$(npm bin -g):$PATH"

# Recommended for a project
npm install --save-dev typescript
npx tsc --version

Key rule: npm root -g is for package files; npm bin -g is for commands such as tsc.

FAQ

Why does tsc: command not found happen after installing TypeScript?

Usually, npm installed TypeScript but its global executable directory is not in your shell’s PATH. Verify with npm list -g --depth=0 typescript and npm bin -g.

How do I check whether TypeScript is installed globally?

Run:

npm list -g --depth=0 typescript

If it displays typescript@..., npm has it in the global package list.

Where is the tsc executable installed?

Run:

npm bin -g

The tsc executable should be in that directory. Check it with:

ls -l "$(npm bin -g)/tsc"

What is the difference between npm root -g and npm bin -g?

npm root -g shows where package source files are stored. npm bin -g shows where runnable commands, including tsc, are linked or created.

Should I install TypeScript globally or locally?

Use a local development dependency for projects so the project controls its TypeScript version. A global installation is optional for convenience.

How can I run TypeScript without a global installation?

Install it in the project and run:

npx tsc --version

Or define an npm script and run it with npm run.

Do I need to restart the terminal after changing PATH?

Yes. Open a new terminal, or reload the relevant shell configuration file, such as source ~/.bash_profile for Bash.

Take Quiz

Mini Project

Description

Create a small TypeScript project that checks an array of task names and prints each task. The project uses a local TypeScript installation and an npm script, so it does not depend on a globally available tsc command.

Goal

Set up and run npm run build to compile a TypeScript file into JavaScript.

Requirements

  1. Create a new folder and initialize it as an npm project.
  2. Install TypeScript as a development dependency.
  3. Add a build script that runs the TypeScript compiler.
  4. Create a TypeScript source file in a src folder.
  5. Compile the source into a dist folder and run the generated JavaScript with Node.js.
Take Quiz

Keep learning

Related questions

View more