由于某些原因,npx tsc --init
打印出以下错误:
$ npx tsc --init
npx: installed 1 in 1.467s
error TS5023: Unknown compiler option 'init'.
我已经用Yarn 2安装了typescript
软件包:
$ 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
有人可以向我解释为什么tsc
无法识别--init
以及我在做什么错吗?
UPDATE:
如Daniel所指出的,问题在于npx
找不到或识别随Yarn 2安装的typescript
程序包。解决方案是改用yarn
:yarn tsc --init
从npx tsc --init
的输出来看,您似乎没有在运行命令的目录中安装typescript
软件包。 npx
试图通过安装命令运行所需的任何软件包来提供帮助。
尽管它试图提供帮助,但最终没有安装到2020年的软件包。如果运行$ npx tsc -v
,则很可能会得到以下输出:
$ npx tsc -v
npx: installed 1 in 1.098s
message TS6029: Version 1.5.3
但是,如果您安装了typescript
软件包,则应该获得此软件包:
$ npx tsc -v
Version 3.9.3
如您所见,npm
安装的版本不同。这是因为npx
最终安装了tsc
软件包而非typescript
。 tsc
程序包还提供了tsc
命令。 npx
选择它而不是typescript
,因为尽管两个程序包都提供了tsc
命令,但它也被称为tsc
。 npx
认为这是更合适的选择。
UPDATE:
纱线2引入了Plug'n'Play
功能。依赖项的安装与纱线1和npm
以前的安装方式非常不同。
纱线1和npm
将您的程序包的代码放在每个项目的node_modules
目录中。 npx
去那里查找命令。
另一方面,纱线2将软件包代码安装在共享位置,并将单个.pnp.js
文件放置在您的项目中,该文件执行映射。如果您为一个项目安装软件包,则在另一个项目中使用该软件包则无需再次下载。
但是,任何依赖于node_modules
的工具都将被破坏。这就是npx
在您的项目中找不到typescript
的原因。 npx
不知道Plug'n'Play
。
您可以在此处阅读有关此功能的更多信息:https://yarnpkg.com/features/pnp