本文介绍了“npx tsc --version"在虚拟机内报告不同的 TypeScript 版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在我的主机 + 来宾操作系统上的项目上运行 npx tsc.但是来宾使用的是不同(旧)版本的 tsc - 我不确定它来自哪里.

I want to be able to run npx tsc on my project on both my host + guest operating systems. But the guest is using a different (older) version of tsc - and I'm not sure where it's coming from.

我的设置:

  • 主机操作系统:Windows 10
  • 来宾操作系统:Debian 9
  • 我正在使用 VirtualBox,并且来宾正在使用 VirtualBox 的共享文件夹"功能挂载主机的文件 - 因此它没有项目文件的单独副本 - 我的项目始终通过共享文件夹访问.
  • 我没有在主机或来宾操作系统上全局安装 Typescript (npm -g)(为了确认这一点,在主机+来宾上运行 npm -g ls typescript 显示空",并且按预期单独运行tsc"不起作用).
  • Host OS: Windows 10
  • Guest OS: Debian 9
  • I'm using VirtualBox, and the guest is mounting the host's files using VirtualBox's "shared folders" feature - so it doesn't have a separate copy of the project files - my project is accessed through shared folders at all times.
  • I do NOT have Typescript installed globally (npm -g) on either the host or guest OS (to confirm this, running npm -g ls typescript on both host+guest shows "empty", and running "tsc" alone does not work, as expected).

我使用 NPM 将 TypeScript 3.3.3333 安装到项目中.

I have a project with TypeScript 3.3.3333 installed into the project with NPM.

在 Windows 主机操作系统上,当我 cd 到项目文件夹并运行时:

On the Windows host OS, when I cd to the project folder and run:

  • npm ls typescript 我看到输出:[email protected](如预期)
  • npx tsc --version 我看到输出:Version 3.3.3333(如预期)
  • npm ls typescript I see output: [email protected] (as expected)
  • npx tsc --version I see output: Version 3.3.3333 (as expected)

在 Linux 客户机操作系统中,当我 cd 到项目文件夹并运行时:

Inside the Linux guest OS, when I cd to the project folder and run:

  • npm ls typescript 我看到输出:[email protected](如预期)
  • npx tsc --version 我看到输出:message TS6029: Version 1.5.3(意外!)
  • npm ls typescript I see output: [email protected] (as expected)
  • npx tsc --version I see output: message TS6029: Version 1.5.3 (unexpected!)

所以我无法运行 npx tsc 来在客户机中编译我的代码,因为它不支持我的一些较新的 tsconfig 设置.

So I'm unable to run npx tsc to compile my code inside the guest, as it doesn't support some of my newer tsconfig settings.

这个 tsc 1.5.3 版本来自哪里,我该如何摆脱它?

Where could this tsc 1.5.3 version be coming from, and how do I get rid of it?

或者是否有一些我可以在主机上运行的替代 NPM 命令,将可用的 tsc 安装到适用于 Windows + Linux 的项目中?

Or is there some alternative NPM command I can run on the host that will install a usable tsc into the project that works for both Windows+Linux?

此外,above 我的项目根目录中的所有父文件夹都没有 node_modules 文件夹(但当然我的项目根目录确实有它的 node_modules子文件夹).

Also, none of the parent folders above my project's root have a node_modules folder (but of course my project's root does have its node_modules sub-folder).

推荐答案

TypeScript 二进制文件被称为 tsc 为简洁起见.当它不是全局安装时,npx 无法知道 tsc 指的是 typescript 包的 tsc 二进制文件.npx tsc 指的是 已弃用的 tsc.

TypeScript binary is called tsc for shortness. When it's not installed globally, npx has no way to know that tsc refers to tsc binary for typescript package. npx tsc refers to deprecated tsc package.

解决这个问题的一种方法是明确指定包名:

A way this can be fixed is to specify package name explicitly:

npx -p typescript tsc

这里的实际问题是项目依赖于全局 TypeScript 安装.在项目依赖项中使用 typescript 编写项目并在 package.json NPM 脚本中引用本地安装是很常见的:

And the actual problem here is that the project relies on global TypeScript installation. It's common to have typescript a project was written with in project dependencies and refer to local installation in package.json NPM scripts:

...
"scripts": {
  "build": "tsc"
},
"devDependencies": {
  "typescript": "~3.3.0"
"
...

这篇关于“npx tsc --version"在虚拟机内报告不同的 TypeScript 版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 20:46