Fix mismatching tsc
binary versions in expo-module-scripts in monorepos
#27779
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why
If you have a monorepo in which you have different versions of typescript installed you may see that
expo-module build plugin
fails because of unknown TS compiler options. I think it happens because wrong version oftsc
picked up bynpx
.Related GitHub issues:
npm/cli#6765
How
As far as I understand the root cause of this issue is that if we simply run
npx tsc ...
npm does not knowtsc
binary belongs to thetypescript
package and it will simply try to resolve it from the root workspace'snode_modules/.bin
where it will find the wrong version oftsc
because of dependency hoisting. It should start looking uptsc
in the workspace'snode_modules/.bin
(from where we're executingexpo-module build plugin
) but that is not the case for some reason. We can only achieve this by giving a hint tonpx
thattsc
belongs to the package calledtypescript
. In this case it will start the binary lookup from the workspace'snode_modules/.bin
folder and use the correct version to build our module plugin.Test Plan
packages/cool-module
) by following the official tutorial with a config plugin (https://docs.expo.dev/modules/config-plugin-and-native-module-tutorial/)typescript@^4.5.5
to your workspace root as a dev dependency (or add you could have multiple other workspaces that depend ontypescript@^4.5.5
, e.g.packages/my-api
)tsc
is installed in your rootnode_modules
and 5.3.0tsc
is installed in yourpackages/cool-module/node_modules
folder.cd packages/cool-module && yarn run build plugin
you should receive TypeScript config errors sincepackages/cool-module/plugin/tsconfig.json
inherits configuration options that are unknown to 4.5.5tsc
. (e.g."lib": ["es2023"]
). At this point you know something is wrong sinceexpo-module-scripts
depends ontypescript": "^5.1.3"
that should be aware ofes2023
andnode16
module resolution logic.cd packages/cool-module
) and execute the same command that is executed byexpo-module build plugin
under the hood:yarn exec -- npx tsc --version
. It should give the mismatched4.5.5
version.packages/cool-module
) and execute now the following command:yarn exec -- npx --package=typescript tsc --version
. It should give the correct5.3.0
version.Checklist
npx expo prebuild
& EAS Build (eg: updated a module plugin).