Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"npx tsc --version" reports different TypeScript version inside virtual machine

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: [email protected] (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: [email protected] (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).

like image 330
LaVache Avatar asked Mar 13 '19 09:03

LaVache


People also ask

What does npx tsc do?

The npx tool can be used to temporarily install TypeScript and run the compiler. Let's break this command down: This command first downloads and installs the typescript npm package. tsc is the executable name of the TypeScript compiler.

How can I tell what version of TypeScript is on my project?

Go to: C:\Program Files (x86)\Microsoft SDKs\TypeScript, there you see directories of type 0.9, 1.0 1.1. Enter the high number that you have (in this case 1.1) Copy the directory and run in CMD the command tsc -v, you get the version.

How can I tell what version of TypeScript is local?

Test that the TypeScript is installed correctly by typing tsc -v into your terminal or command prompt. You should see the TypeScript version print out to the screen. For help on possible arguments you can type tsc -h or just tsc .

How do I get TSC TypeScript?

TypeScript is available as a package on the npm registry available as "typescript" . You will need a copy of Node. js as an environment to run the package. Then you use a dependency manager like npm, yarn or pnpm to download TypeScript into your project.


1 Answers

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"
"
...
like image 125
Estus Flask Avatar answered Oct 10 '22 05:10

Estus Flask