Tsc is not recognized as an internal or external command – How to solve it?

Tsc is not recognized as an internal or external command – How to solve it?

The error “tsc is not recognized as an internal or external command” relates to install TypeScript. Solve this error completely easily. So how to do it? Let’s go into detail now.

The reason for this error

The error happens when you try to use some ‘tsc’ command, but your device doesn’t have TypeScript or the installation fails.

Here I try to transform a TypeScript file to a JavaScript file:

tsc index.ts

But my device doesn’t have TypeScript yet so I will get the error:

'tsc' is not recognized as an internal or external command,
operable program, or batch file.

The solutions for the error “tsc is not recognized as an internal or external command”

Install TypeScript

You can download and follow this command if you haven’t had TypeScript yet.

npm install typescript@latest –g

Here I install TypeScript’s latest version in global scope, which you can use in any project without having to install TypeScript again. You can read more about it here.

added 1 package, and audited 3 packages in 3s

found 0 vulnerabilities

Here is how it shows when I download. You can check if install success by this command:

>tsc --version
Version 4.8.4

If it shows something like that, your download has been successful.

If you download fail you can use this command:

sudo npm install typescript@latest –g

Then pass in the password of your device.

Initialize tsconfig.json file

The tsconfig.json file is a file that allows you to set your TypeScript project files. To initialize tsconfig.json file, you can use this command:

tsc --init

Result:

Created a new tsconfig.json with:
                                                                        
TS
  target: es2016
  module: commonjs
  strict: true
  esModuleInterop: true
  skipLibCheck: true
 
forceConsistentCasingInFileNames: true

You can learn more at https://aka.ms/tsconfig

Now we can use the ‘tsc’ command. Suppose I want to transform a TypeScript file to a JavaScript file.

index.ts

const anArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log("Hello From Learn Share IT");
const sum = anArray.reduce((pre, curr) => pre + curr, 0);
console.log(sum);

To transform to a JavaScript file I use:

tsc index.ts

Then I get a JavaScript file with the same name and have this content:

index.js

var anArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log("Hello From Learn Share IT");
var sum = anArray.reduce(function(pre, curr) { return pre + curr; }, 0);
console.log(sum);

To run index.js code, I use:

node index.js

Output:

Hello From Learn Share IT
45

Summary

In this article, I’ve shown you how to solve the error “tsc is not recognized as an internal or external command”. It happens when you haven’t installed TypeScript. You can install by command npm install typescript@latest –g. Good luck to you!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *