question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Resolving hoisted `typeRoots` paths

See original GitHub issue

TypeScript Version: 3.6.2

Search Terms:

  • resolve typeRoots in parent package node_modules
  • sharing tsconfig in yarn workspaces
  • use hoisted tsconfig in child package

Code

I’ve set up a Lerna monorepo which uses Yarn Workspaces. Within this monorepo is a config package, which other packages depend on. This config package has @types/node and @types/jest as dependencies.

From sibling packages, the shared config is used like so:

{
  "extends": "@project/config/ts.json",
  "include": ["src", "tests"],
  "compilerOptions": {
    "outDir": "lib"
  }
}

In the shared config, I point to the appropriate types root:

{
  "compilerOptions": {
    // ...
    "typeRoots": ["node_modules/@types"]
  }
}

However, within the shared config package, @types/* have been hoisted from the local node_modules to the monorepo’s top-level node_modules. This behavior is usually fine given node’s resolution… but it seems the config does not resolve to its parent’s node_modules.

Expected behavior:

For the specified type root to resolve to the parent’s node_modules

Actual behavior:

Tests fail with messages such as Cannot find name 'describe'. Do you need to install type definitions for a test runner? ...

One workaround:

{
  "compilerOptions": {
    // ...
    "typeRoots": ["../../node_modules/@types"]
  }
}

This workaround isn’t very clean. Lerna & Yarn definitely pose some complexity for type root resolution. Hopefully resolving parents is considered a worthwhile modification.

Please let me know. Thank you 😃

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:32
  • Comments:9

github_iconTop GitHub Comments

8reactions
nageycommented, Feb 24, 2020

This is one that we had been resolving with a workaround similar to the one that @harrysolovay proposes. Unfortunately, we quickly ran into an issue there with different versions of a @types package. Our particular situation was the internal-package-a had a dependency on @types/node@12.0.0 where another had one on @types/node@13.1.17. This led to a case where in internal-package-a both node_modules and ../../node_modules(at the monorepo root) had a copy of @types/node, and was causing build failures.

We’ve managed to resolve this for the short term by homologating our versions of this type package, but I can see the day coming where we won’t be able to do that and it’ll lead to a crufty, byzantine set of tsconfig rules that expect certain packages in the package root versus the monorepo root (which is exactly the sort of thing we’re looking to avoid).

Very much looking forward to some positive movement on this issue.

3reactions
DeanMaurocommented, Nov 15, 2021

I had the same issue. Solved by omitting typeRoots entirely, which exposes ./node_modules/@types, ../node_modules/@types, etc. by default.

https://www.typescriptlang.org/tsconfig#typeRoots image

Read more comments on GitHub >

github_iconTop Results From Across the Web

TSConfig Reference - Docs on every TSConfig option
All relative paths found in the configuration file will be resolved relative ... If typeRoots is specified, only packages under typeRoots will be...
Read more >
How to set up a TypeScript monorepo and make Go ... - Medium
A solution with path aliases is presented and two solutions, each with its ... can't be hoisted in the root config because they...
Read more >
TypeScript + Yarn Workspace Monorepo - DEV Community ‍ ‍
Run yarn install again in root project, yarn will hoist the same dependencies among sub projects to the top level node_modules .
Read more >
TypeScript - Parcel
See Babel in the JavaScript docs for more details. Resolution. #. Parcel does not currently support the baseUrl or paths options in tsconfig.json...
Read more >
tsconfig.json typeroots custom path not picked up
I found the config that fixes this. Note the paths and baseUrl properties: { "version": "2.1.5", "compilerOptions": { "module": "commonjs", ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found