Search code examples
node.jsnpmcommand-line-interfacenpx

How to run NodeJS CLI tools without having to type npx


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?


Solution

  • ! 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).