279

I want to install typescript, so I used the following command:

npm install -g typescript 

and test tsc --version, but it just show 'tsc command not found'. I have tried many ways as suggested in stackoverflow, github and other sites. but it doesn't work. How could I know typescript is installed and where it is.

my OS is Unix, OS X El Capitan 10.11.6, node version is 4.4.3, npm version is 3.10.5

6
  • 6
    Most likely you have a problem with your node/npm setup; for example, the relevant npm directory is not on your path. Where typescript is installed depends on your environment, but you could start with npm list -g. Or try npm config get prefix (your packages will normally be in the bin directory under this). Also see this question.
    – user663031
    Sep 9, 2016 at 6:23
  • Thanks you , I got it. I forgot to export prefix path.
    – Aung Thet
    Sep 9, 2016 at 14:23
  • You can also check my answer Jul 26, 2020 at 19:24
  • I got it working switching to yarn Aug 7, 2021 at 16:42
  • 3
    In my case I needed to do node_modules/.bin/tsc --version ✍🏻
    – Jonny
    May 12, 2022 at 1:19

30 Answers 30

279

A few tips in order

  • restart the terminal
  • restart the machine
  • reinstall nodejs + then run npm install typescript -g

If it still doesn't work run npm config get prefix to see where npm install -g is putting files (append bin to the output) and make sure that they are in the path (the node js setup does this. Maybe you forgot to tick that option).

10
  • 6
    Thanks, I got it. Sure , I forgot to assign path `export PATH=/prefix'sPath/bin:$PATH' and (docs.npmjs.com/getting-started/fixing-npm-permissions) also this site suggest like this. I used a whole day for this :).
    – Aung Thet
    Sep 9, 2016 at 14:21
  • 1
    stackoverflow.com/a/46783952/495157 - This answer is more informative - especially if you do Angular/Ionic development.
    – JGFMK
    Feb 15, 2019 at 20:13
  • 1
    I had to uninstall node/npm. Used brew to install NVM and then installed node using nvm install node then sudo npm install -g typescript to get tsc --version to work
    – alex
    Mar 9, 2019 at 18:31
  • 15
    I did only "npm install typescript -g" and it worked perfectly
    – pixparker
    Nov 8, 2019 at 16:51
  • 2
    Upgrading node-version helped me. Jun 27, 2020 at 13:45
209

You are all messing with the global installations and -path files. Just a little error might damage every project you have ever written, and you will spend the rest of the night trying to get a console.log('hi') to work again.

If you have run npm i typescript --save-dev in your project - just try to run:

npx tsc 

And see if it works before messing with global stuff (unless of course you really know what you are doing)

4
  • 32
    Finally an answer without global installations Feb 27, 2021 at 15:17
  • 4
    Agree, this should be the accepted answer! To init a new npm package, run those 3 commands: npm init -y && npm i typescript --save-dev && npx tsc --init
    – Hans
    Sep 25, 2021 at 20:20
  • Thanks for taking into account global installations. Though I want to add that 'npx tsc foo.ts' is needed to compile TS to JS
    – emre-ozgun
    Feb 8, 2022 at 14:08
  • Why does this work when npm run tsc and npm tsc do not work? May 2, 2023 at 21:05
99

I had to do this:

npx tsc app.ts
3
  • 2
    This helped me, but to clarify, I had to do npm install --save-dev typescript && npx tsc --version && npx tsc <filename>. This installed tsc, successfully tested it with --version, and successfully compiled my ts file to a js file.
    – Jason
    Sep 10, 2019 at 20:10
  • 2
    This worked, but i just used npx tsc. Adding tsc probably resolves need to use npx. Jul 28, 2020 at 16:48
  • 1
    Why is that some have to use 'tsc' with npx and for others it works without npx? Mar 21, 2022 at 0:13
35

After finding all solutions for this small issue for macOS only.
Finally, I got my TSC works on my MacBook pro.

This might be the best solution I found out.

For all macOS users, instead of installing TypeScript using NPM, you can install TypeScript using homebrew.

brew install typescript

Please see attached screencap for reference. enter image description here

2
  • After 12 hours this works for me :) just following your post. Thank you so much @Marjun
    – Prashen
    Dec 30, 2021 at 11:12
  • This is what worked for me and not npm i typescript -g . Probably has to do with my path running through brew for nvm
    – Curtis M
    Apr 29, 2023 at 17:42
19

Globally installing TypeScript package worked for me.

npm install typescript -g
11

If your TSC command is not found in MacOS after proper installation of TypeScript (using the following command: $ sudo npm install -g typescript, then ensure Node /bin path is added to the PATH variable in .bash_profile.

Open .bash_profile using terminal: $ open ~/.bash_profile;

Edit/Verify bash profile to include the following line (using your favorite text editor):

export PATH="$PATH:"/usr/local/lib/node_modules/node/bin"";

Load the latest bash profile using terminal: source ~/.bash_profile;

Lastly, try the command: $ tsc --version.

0
9

For mac users, you don't need to restart your laptop or doing any other commands

Use brew install typescript

7

Use:

npm rebuild typescript

This will rebuild the tsc link on your machine.

3
  • 1
    This was what helped me after banging my head against multiple install/uninstalls of tsc... Mar 2, 2023 at 12:36
  • I didn't want to fix the issue by installing typescript globally. This WORKED
    – TeachMe
    Mar 21 at 12:45
  • Worked a charm!
    – James Amos
    Apr 12 at 9:22
6

First Install typescript by running this command npm install typescript or npm install -g typescript [ for installing typescript globally ] then run npx tsc --version for checking version of Typescript, instead of tsc --version.

Same goes to running any typescript file, just run npx tsc <filename>.ts For example, for a file named hello.ts, run npx tsc hello.ts.

0
5

This answer is specific for iTermV2 on MAC

  1. First of all, I needed to instal as sudo (admin) during NPM install

    sudo npm install -g typescript

  2. NPM installs the libraries under /usr/local/Cellar/node/<your latest version>/lib/node_modules/typescript folder and symlinks at /usr/local/Cellar/node/<your latest version>/bin/tsc

hence I went ~/.zshrc ( .bashrc, if you use bash)and added /usr/local/Cellar/node/<your latest version>/bin to the $PATH.

  1. reload the shell profile via source ~/.zshrc (.bashrc in your case)
1
  • 1
    Hm good to know. But who takes care of these links from now on? Feb 2, 2020 at 13:55
4

In package.json

  "scripts": {
    "tsc": "./node_modules/typescript/bin/tsc",
    "postinstall": "npm run tsc"
  },

Works for me for Heroku deployment. Installing typescript npm install -D typescript and writing tsc in the build script "build": "tsc", does not work for me.

3

I had this same problem on Ubuntu 19.10 LTS.

To solve this I ran the following command:

$ sudo apt install node-typescript

After that, I was able to use tsc.

0
2

Easy fix for Mac I found. Just run these commands:

sudo npm install -g concurrently
sudo npm install -g lite-server
sudo npm install -g typescript

Nothing worked except this for me.

1
  • 3
    How does this answer have so many upvotes? Please don't do this. First of all, the first two packages have nothing to do with this problem and second of all, you should not be using sudo to install packages globally. Check the comment on this post: stackoverflow.com/a/57119888/163799
    – Loolooii
    Nov 29, 2021 at 14:36
2

The only solution that work for me was put npx tsc -v or for the compiling npx tsc salida.ts

"salida.ts" is the name of the file

2
  1. Check your npm version

  2. If it's not properly installed, then install it first

  3. run this command npm install typescript -g

  4. now tsc <file_name>.ts

  5. It'll create a corresponding .js file. eg <file_name>.js

  6. now try node <file_name>.js

2

For windows and yarn user, try yarn tsc --init

1

None of above worked for me.

I tried this as well,

yum install typescript 

was able to compile by hook and crook as follows. Not recommended but just a workaround.

Just install locally using npm, as npm install typescript and verify in node_module folder, if its downloaded. and then run,

./node_modules/typescript/bin/tsc  --help
./node_modules/typescript/bin/tsc  //this line actually runs and compile and generate the compiled file. 
1

Don't forget to use sudo if you are using Linux.

sudo npm install -g typescript 
0

Non-admin solution

I do not have admin privileges since this machine was issued by my job.

  • get path of where node modules are being installed and copy to clipboard
    • npm config get prefix | clip
    • don't have clip? just copy output from npm config get prefix
  • add copied path to environment variables
    • my preferred method (Windows)
    • (Ctrl + R), paste rundll32 sysdm.cpl,EditEnvironmentVariables
    • under User Variables, double-click on Path > New > Paste copied path
0

None of above answer solve my problem. The fact is that my project did not have type script installed. But locally I had run npm install -g typescript. So I did not notice that typescript node dependency was not in my package json.

When I pushed it to server side, and run npm install, then npx tsc I get a tsc not found. In facts remote server did not have typescript installed. That was hidden because of my local global typescript install.

0

On Windows 10 i solved it by adding %APPDATA%\npm to the path

0

I have tried a lot to deploy the Node.js typescript project on Heroku and I have tried different solutions but none of them working for me. So, I have implemented a solution that is to create a build locally which is a dist folder, and just only push dist folder with package.json files, you don't need to push your src folder to Heroku. and in your script add "start": "node dist/index.js"

Here are my project structure:
Here are my project structure:

.gitignore file:
.gitignore file:

package.json file:

"start": "node dist/index.js",
"deploy": "tsc && git add . && git commit -m Heroku && git push heroku master",
"dev": "ts-node-dev --respawn --pretty --transpile-only src/index.ts"

just need to add these scripts:
just need to add these scripts:

0

I was having trouble with this because I didn't want to globally install typescript. I found I had to add a script to the package.json that called tsc for me. The solution can be found here - https://stackoverflow.com/a/41446584/6301243

0

I apply many things but these two are the way which might be work for you:

  • Reinstall npm with a node version manager (recommended)
  • Manually change npm's default directory
0

For pnpm:

I was using pnpm and the same error occurred to me, which I fixed using npx tsc index.js

0

One possible culprit to this could be having a typo in your dependency key at package.json, when running yarn in your monorepo root does not install typescript.
In my case I manually added a new lib to my monorepo and then tried to type "devDependencies" into package.json, but instead ended up with "devDependecies".

To fix this, install typescript from your terminal at your monorepo root:
yarn workspace @myorg/mylib -D typescript

0

Just try to install typescript globally before your script, something like as follows

Yml file sample

 script:
   - cd utils
   - npm install -g typescript
   - tsc ../utils/<your-script>.ts
   - node ../utils/<your-script> 
-1

For windows:

Add the path by using command as below in command prompt:
path=%path%;C:\Users\\npm

As in my case, the above path was not registered for command.

%userprofile% in run windows, will give you path to C:\users\

-1

I solved this on my machine by just running sudo npm install in the directory that I was getting the error.

1
  • 1
    You shouldn't be forced to run sudo for installning packages. Then you should reinstall npm. See more Do not sudo npm
    – GLindqvist
    Oct 18, 2019 at 14:59
-5

This works perfectly on Mac. Tested on macOS High Sierra

sudo npm install -g concurrently
sudo npm install -g lite-server
sudo npm install -g typescript
tsc --init

This generates the tsconfig.json file.

1
  • 1
    concurrently and lite-server have little to nothing to do with being able to run the tsc command. Please do not advise installing unnecessary dependencies.
    – andras
    Mar 20, 2020 at 9:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.