Jesuspc
Jesuspc

Reputation: 1734

Typescript module was successfully resolved but cannot be found

As part of a Typescript 3 monorepo I was trying to reference a package (project_2) from another one (project_1) in the same repo. The repo looks like:

|- project_1
|    |- package.json (@projects/project_1)
|    |- tsconfig.json
|    |- src
|        |- foo.ts
|- project_2
     |- package.json (@projects/project_2)
     |- tsconfig.json
     |- src
         |- bar.ts

foo.ts references bar.ts with the following line:

import { bar } from "@projects/project_2/src/bar";

When I run

cd project_1 && yarn exec tsc -- --traceResolution

Starting in Typescript 2.9 I get the following message:

======== Resolving module '@projects/project_2/src/bar' from '/home/jesuspc/Code/test2/project_1/src/foo.ts'. ======== Module resolution kind is not specified, using 'NodeJs'. 'baseUrl' option is set to '/home/jesuspc/Code/test2/', using this value to resolve non-relative module name '@projects/project_2/src/bar'. 'paths' option is specified, looking for a pattern to match module name '@projects/project_2/src/bar'. Module name '@projects/project_2/src/bar', matched pattern '@projects/'. Trying substitution './', candidate module location: './project_2/src/bar'. Loading module as file / folder, candidate module location '/home/jesuspc/Code/test2/project_2/src/bar', target file type 'TypeScript'. File '/home/jesuspc/Code/test2/project_2/src/bar.ts' exist - use it as a name resolution result. ======== Module name '@projects/project_2/src/bar' was successfully resolved to '/home/jesuspc/Code/test2/project_2/src/bar.ts'. ======== src/foo.ts:1:24 - error TS2307: Cannot find module '@projects/project_2/src/bar'.

1 import { bar, x } from "@projects/project_2/src/bar";

So apparently the module was resolved properly:

"Module name '@projects/project_2/src/bar' was successfully resolved to '/home/jesuspc/Code/test2/project_2/src/bar.ts'"

but then it failed with:

@projects/project_2/src/bar not found

which does not make sense to me.

I have noticed that the module gets resolved properly in Typescript 2.8 and the compilation succeeds. I haven't been able to find any relevant change between 2.8 and 2.9 in the patch notes.

Why is it reporting that the module location is properly resolved first to then complain that it can not been found?

I have pushed the code to reproduce the issue to this repo.

Upvotes: 4

Views: 503

Answers (1)

Karol Majewski
Karol Majewski

Reputation: 25790

What I did:

  • In project_1, changed the path to your dependency to "@projects/project_2": "../project_2"
  • Still in project_1, installed @types/node

Now when I run yarn exec tsc it works as expected.

✨  Done in 3.48s.

Upvotes: 2

Related Questions