Reputation: 657
My project has a structure like this:
workspace
|__pkg1
|__tsconfig.json
|__package.json
|__ts/
|__pkg2
|__tsconfig.json
|__package.json
|__ts/
|__lerna.json
|__package.json
|__tsconfig.json
In workspace/package.json, I specified the typescript version to be "^4.2.3". To install the dependency for all packages, run the command under workspace directory:
npm install & lerna bootstrap && lerna run build
"lerna bootstrap" will install all the dependencies for pkg1 and pkg2, "lerna run build" (equivalent to run "npx tsc" in pkg1 and pkg2) will compile typescript in ts/ folder from pkg1 and pkg2. It turned out the tsc version in workspace directory is indeed 4.2.3, but in pkg1 and pkg2, the typescript version is 4.5.2
cd pkg1 && npx tsc -v # output is Version 4.5.2
I have tried (1) to change "^4.2.3" to "4.2.3" in workspace/package.json, (2) specify typescript version explicitly in package.json from pkg1 and pkg2. (3) remove all package-lock.json files. However, neither of them work. How can I fix it?
In practice, I need build a clean docker image for this project. It have multiple pkgs, so I don't want to install typescript one by one in each of the package directory.
Upvotes: 1
Views: 5442
Reputation: 81
I believe your issue might be the result of npm's versioning paradigm, which is both straightforward and confusing.
I just experienced a similar issue. My package.json
specified typescript version ^4.6.2
, but my package-lock.json
always ended up being version 4.9.4
.
I resolved the issue by setting the typescript version in my package.json
to ~4.6.2
. This ensures that npm upgrades my typescript package to a newer patch version but never a newer minor version. After changing the caret (^
) to a tilde (~
), I ran npm install
, and my package-lock.json
's typescript version sat comfortably at 4.6.4
.
To provide a brief summary of package upgrade convention for npm (which, I might add, is covered ad nauseam but somehow trips up people all the time, including me):
{major}.{minor}.{patch}
.^
instructs npm to upgrade to the latest minor version.~
instructs npm to upgrade to the latest patch version.What happens when there's no tilde or caret? I suspect it instructs npm to chose that specific version, but seeing as that didn't work for you, maybe I'm wrong? Like I said, straightforward and confusing.
This Stack Overflow answer explains package.json
versioning meaning in greater detail.
Upvotes: 2
Reputation: 87
try removing all the code which is related to typescript from the package-lock.json and npm install again and check. I think this should solve the issue as I have too experienced this before.
Upvotes: 0