“npx tsc --version” reports different TypeScript version inside virtual machine

…衆ロ難τιáo~ 提交于 2021-02-09 02:56:09

问题


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.

My setup:

  • 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).

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

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

  • npm ls typescript I see output: typescript@3.3.3333 (as expected)
  • npx tsc --version I see output: Version 3.3.3333 (as expected)

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

  • npm ls typescript I see output: typescript@3.3.3333 (as expected)
  • npx tsc --version I see output: message TS6029: Version 1.5.3 (unexpected!)

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.

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

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?

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).


回答1:


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

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"
"
...


来源:https://stackoverflow.com/questions/55138134/npx-tsc-version-reports-different-typescript-version-inside-virtual-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!