尝试执行“npx typescript --init”“npm ERR!无法确定要运行的可执行文件”

Jim*_*ode 13 node.js npm typescript yarnpkg

错误是什么意思npm ERR! could not determine executable to run意思?

\n

我正在尝试使用打字稿和纱线初始化一个简单的nodejs应用程序。我以前从未真正使用过纱线。

\n

我运行了命令:\n yarn init\n成功创建了一个package.json\n npx typescript --init \n给了我上面的错误消息。

\n

版本:\nyarn v1.22.11\nnpx 7.12.0

\n

我跑了yarn add typescript,它给了我:

\n
info Direct dependencies\n\xe2\x94\x94\xe2\x94\x80 typescript@4.3.5\ninfo All dependencies\n\xe2\x94\x94\xe2\x94\x80 typescript@4.3.5\n
Run Code Online (Sandbox Code Playgroud)\n

但当我尝试时typescript -v我得到typescript: command not found

\n

Jim*_*ode 33

修复方法是使用命令npx tsc --init

我不知道为什么我遵循的教程使用了npx typescript --init,但它对他们有效,但对我无效。


Fel*_*cia 10

扩展JimmyTheCode 的答案,它之所以有效,是因为tsc它是一个仅安装打字稿的虚拟包。

在不安装另一个软件包的情况下解决此问题的正确方法是使用-p|--package上的选项npx。当从包中调用特定的二进制文件时,此标志非常有用。

npx --package typescript tsc --init
# OR
npx -p typescript tsc --init
Run Code Online (Sandbox Code Playgroud)

您可以在npx 的文档中找到更多信息。