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 fromPATH. - 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 foundeven 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.
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 , use:
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:
npm install -g typescriptdownloads and installs the globaltypescriptpackage.npm list -g --depth=0 typescriptasks npm whether that package exists in the global package list.npm bin -gprints the directory where npm places global executable commands.ls -l "$(npm bin -g)/tsc"checks whether thetsccommand file was created in that directory."$(npm bin -g)/tsc" --versionruns that exact file directly, without relying onPATH.
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:
Real World Use Cases
TypeScript compilation is commonly used in several workflows:
- Frontend applications: Compile
.tsand.tsxsource files into JavaScript before browser deployment. - Node.js APIs: Type-check server code during development and build JavaScript for production.
- CI pipelines: Run
tsc --noEmitto 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.
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 problems.
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.
Comparisons
| Approach | Command example | Best use | Main consideration |
|---|---|---|---|
| Global TypeScript installation | npm install -g typescript | Personal command-line use | Requires npm global bin directory in PATH |
| Local project installation | npm install -D typescript | Applications, libraries, teams, CI | Version is controlled by the project |
| npm script | npm run build | Repeatable project tasks | Requires a script in package.json |
npx execution | npx tsc --version | Running the local tool without a script |
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. shows where runnable commands, including , are linked or created.
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
- Create a new folder and initialize it as an npm project.
- Install TypeScript as a development dependency.
- Add a
buildscript that runs the TypeScript compiler. - Create a TypeScript source file in a
srcfolder. - Compile the source into a
distfolder and run the generated JavaScript with Node.js.
Keep learning
Related questions
@Directive vs @Component in Angular: Differences, Use Cases, and When to Use Each
Learn the difference between @Directive and @Component in Angular, including use cases, examples, and when to choose each.
Accessing Input Value from EventTarget in TypeScript
Learn why EventTarget has no value property in TypeScript and safely read values from HTML input events in Angular applications.
Angular (change) vs (ngModelChange): What’s the Difference?
Learn the difference between Angular (change) and (ngModelChange), when each fires, and which one to use in forms and inputs.