tsc does not recognize '--init' option
Judging from the output of npx tsc --init
you do not seem to have the typescript
package installed in the directory where you've run the command. npx
tries to be helpful by installing any packages needed in order for the command to run.
Although it was trying to be helpful it ended up not installing the package one would expect in 2020. If you run $ npx tsc -v
you will most likely get this output:
$ npx tsc -vnpx: installed 1 in 1.098smessage TS6029: Version 1.5.3
If you had the typescript
package installed, however, you would have gotten this, instead:
$ npx tsc -vVersion 3.9.3
As you can see, the version installed by npm
is different. That's because npx
ended up installing the tsc
package and not typescript
. The tsc
package also provides a tsc
command. npx
chose it instead of typescript
because, while both packages provide a tsc
command, it is also called tsc
. npx
thought it was a better fit.
UPDATE:
Yarn 2 introduces the Plug'n'Play
feature. Dependencies are installed very differently from how Yarn 1 and npm
used to.
Yarn 1 and npm
place the code for your packages in the node_modules
directory in every single project. npx
goes to look for commands there.
Yarn 2, on the other hand, installs package code in a shared location and places in your project a single .pnp.js
file that performs the mapping. If you install a package for one project, you won't have to download it again if you use it in another project.
Any tooling that was dependent on node_modules
, however, will be broken. That's why npx
was unable to find typescript
in your project. npx
does not know about Plug'n'Play
.
You can read more about this feature here: https://yarnpkg.com/features/pnp
For others came here which use no yarn
nor npx
, in my case that solve it:
npm install -g typescript --force
EDIT
- I know it doesn't answer the exact question, but it does handle same error.
- Possible explanation of my
npm
solution: something was broken before, maybe installedtsc
instead oftypescript
, or problems with previoustypescript
installation itself.npm install
would install typescript again, and--force
flag is for ignoring existing local files and override them.