1

TL;DR

I cannot execute commands such as tsc unless I include npx before it (e.g npx tsc). How can I fix this?


The title is a bad explanation of the problem I have.

Say I have installed an npm package with a CLI with it (typescript in this example). And the CLI is used like tsc <filename> or just tsc. But whenever I try to do it like that, I get an error like

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

But... when I do

npx tsc

then it works!

So whats the problem?

The problem with doing npx tsc is because

  1. npx is slow at executing commands
  2. its annoying having to type npx and the front of every command.

And the thing is, this was originally not a problem with WSL.

Why dont you just use WSL?

I have always had problems with WSL (primarily permission issues due to security reasons) and so I uninstalled WSL and just used command prompt. I would have perferred using WSL but it was simply not an option.

Other Info:

  • I am using Windows command prompt.
  • I have installed the packages globally

So is there a way to just execute commands that way or is it Command prompts fault?

Pro Poop
  • 357
  • 5
  • 14
  • It seems you've installed the libraries locally instead of global. Global libraries are deployed to the main node folder with proper bat files for easy access (such as `tsc` and other such commands) and work as long as node is in the path environment variable. By installing them locally, you need to use `npx` everywhere. – Alejandro Sep 01 '21 at 23:24
  • @Alejandro right.... I forgot to mention I did install them globally. Sorry about that. – Pro Poop Sep 01 '21 at 23:32
  • 1
    The real question is "why not use npx?" because that's the whole point of it: running project-local CLI tools without polluting your global installation. And you only need `npx` if you want to run things manually, inside npm scripts, you don't. On that note, why would you need to run `tsc` manually instead of having it be part of your project's build script(s)? – Mike 'Pomax' Kamermans Sep 01 '21 at 23:33

1 Answers1

2

! this only works for Windows !

Ok, so I came across this post and thankfully, the first answer there was the solution!

Just add %USERPROFILE%\AppData\Roaming\npm to the path variable in system variables!

To access the system variables, press the Windows key, type Environment variables and click on Environment variables at the bottom of the window. The path variable can be found under User variables for (profile name).

Pro Poop
  • 357
  • 5
  • 14