Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tsc does not recognize '--init' option

For some reason npx tsc --init prints out the following error:

$ npx tsc --init
npx: installed 1 in 1.467s
error TS5023: Unknown compiler option 'init'.

I have installed the typescript package with Yarn 2:

$ yarn add -D typescript
➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed in 0.31s
➤ YN0000: ┌ Fetch step
➤ YN0013: │ typescript@npm:3.9.3 can't be found in the cache and will be fetched from the remote registry
➤ YN0013: │ typescript@patch:typescript@npm%3A3.9.3#builtin<compat/typescript>::version=3.9.3&hash=8cac75 can't be found in the cache and will be fetched from the disk
➤ YN0000: └ Completed in 1.46s
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: Done in 1.95s

Can someone explain to me why tsc does not recognize --init and what i am doing wrong?


UPDATE:

As Daniel figured out, the problem is that npx does not find or recognize the typescript package installed with Yarn 2. The solution was to use yarn instead: yarn tsc --init

like image 855
Don Foumare Avatar asked May 29 '20 00:05

Don Foumare


People also ask

How to fix TSC is not recognized as an internal or external command?

To solve the error "tsc is not recognized as an internal or external command, operable program or batch file", install typescript globally by running npm install typescript@latest -g or prefix tsc with npx, e.g. npx --package typescript tsc --init. One way to solve the error is to use the npx command with the --package flag.

How to fix TSC command not found error in typescript?

To solve the error "tsc: command not found", install the typescript package globally by running npm install typescript@latest -g or use the npx command with the --package flag, e.g. npx --package typescript tsc --init. The fastest way to solve the error is to use the npx command with the --package flag.

How to solve TSC command not found error in NPM?

To solve the error "tsc: command not found", install the typescript package globally by running npm install typescript@latest -g or use the npx command with the --package flag, e.g. npx --package typescript tsc --init. The fastest way to solve the error is to use the npx command with the --package flag. Copied!

How to use TSC instead of NPX?

Now you are able to use the correct tsc command without having to prefix it with npx and use the --package flag. If the global installation of typescript fails, you might have to run the command prefixed with sudo. Copied!


2 Answers

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 -v
npx: installed 1 in 1.098s
message TS6029: Version 1.5.3

If you had the typescript package installed, however, you would have gotten this, instead:

$ npx tsc -v
Version 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

like image 102
Daniel Avatar answered Sep 30 '22 14:09

Daniel


For others came here which use no yarn nor npx, in my case that solve it:

npm install -g typescript --force

EDIT

  1. I know it doesn't answer the exact question, but it does handle same error.
  2. Possible explanation of my npm solution: something was broken before, maybe installed tsc instead of typescript, or problems with previous typescript installation itself. npm install would install typescript again, and --force flag is for ignoring existing local files and override them.
like image 35
Dorad Avatar answered Sep 30 '22 14:09

Dorad