Fixing 'tsc Command Not Found' in TypeScript
TypeScript’s tsc
(TypeScript Compiler) is an essential tool for compiling TypeScript code into JavaScript. If you encounter the error "tsc: command not found
", it typically means TypeScript isn't installed or configured correctly. Here's a step-by-step guide to resolve this issue.
Common Causes of the Error
- TypeScript is not installed.
- TypeScript is installed locally instead of globally.
- The environment variable PATH does not include the TypeScript binary.
Step-by-Step Solution
1. Verify TypeScript Installation
Run the following command in your terminal:
If you see an error, it means TypeScript isn’t installed or is not accessible globally.
2. Install TypeScript Globally
Install TypeScript globally using npm:
After installation, confirm by running:
3. Add TypeScript to Your PATH
If tsc is installed but not recognized, ensure the installation path is added to your system’s PATH.
On Windows:
- Locate the global node_modules folder (e.g.,
C:\Users\<YourUser>\AppData\Roaming\npm
). - Add the folder to your PATH:
- Open "Environment Variables" in System Settings.
- Edit the
Path
variable and include the directory path.
On macOS/Linux:
- Add the npm global directory to your shell configuration:
export PATH=(npm config get prefix)/bin
- Save and restart your terminal.
4. Use npx for Local Installations
If TypeScript is installed locally in a project, you can use npx
to run the tsc
command without a global installation:
5. Check Node.js and npm Installation
Ensure you have Node.js and npm installed. You can verify with:
1 node -v2 npm -v
If either is missing, download and install the latest version of Node.js from https://nodejs.org.
6. Reinstall TypeScript
If issues persist, reinstall TypeScript:
Conclusion
The "tsc: command not found
" error is commonly due to a missing or misconfigured TypeScript installation. By following these steps, you can ensure that tsc
works seamlessly on your system, allowing you to compile TypeScript code without interruption.