tsc无法识别'--init'选项

问题描述 投票:0回答:1

由于某些原因,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程序包。解决方案是改用yarnyarn tsc --init

node.js typescript yarnpkg tsc
1个回答
2
投票

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软件包而非typescripttsc程序包还提供了tsc命令。 npx选择它而不是typescript,因为尽管两个程序包都提供了tsc命令,但它也被称为tscnpx认为这是更合适的选择。

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

© www.soinside.com 2019 - 2024. All rights reserved.