Search code examples
node.jstypescriptyarnpkgtsc

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


Solution

  • 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