3

When I run tsc in the 'proj' directory with --traceResolution, I see the module 'foo' is "successfully resolved" but not "found":

======== Module name 'foo' was successfully resolved to 'C:/dev/ts-options/typings/foo/index.d.ts'. ========

main.ts(1,18): error TS2307: Cannot find module 'foo'.

Setting "noResolve": true in tsconfig.compilerOptions makes the error go away. But I'm supr-confused!

Question 1: What is the difference between "resolution" and "finding"? I thought these were synonyms

Question 2: Why does 'noResolve' affect this behavior? What does noResolve actually do? TS compiler options docs say "Do not add triple-slash references or module import targets to the list of compiled files." but I don't know what that means.

With this directory structure

  • /c/dev/ts-options/
    • typings/
      • foo
        • index.d.ts
    • proj this is where I'm running tsc
      • tsconfig.json
      • main.ts

proj/tsconfig.json (problem is fixed if I change noResolve to false):

{
    "compilerOptions": {
        "baseUrl": ".",
        "types": [],
        "outDir": "../out",
        "paths": {
            "foo": [
                "../typings/foo"
            ],
        },
        "noResolve": true
    },
    "include": [
        "**/*.ts",
    ],
    "exclude": [
        "node_modules"
    ]
}

proj/main.ts:

import foo from "foo"

typings/foo/index.d.ts:

declare function foo(num: number): number;
export default foo;

Update

Two solutions:

  • Set noResolve: false
  • OR add "../typings" to the "include" section in the tsconfig

My guess is that there is a concept of a "compilation unit" such that:

  • with noResolve: false then the compilation unit can grow dynamically as TS follows imports.

  • With noResolve: true, then the compilation unit is fixed to something like tsconfig.baseUrl/** + (tsconfig.include - tsconfi.exclude) .

  • When a file is outside the compilation unit then TS can 'resolve' an import (find out the path to a .d.ts/.ts/.tsx file) but won't 'find' it (actually read the file).

This still doesn't really make sense to me, though. What's the point of 'resolving' to a location one knows one won't be able to 'find'?

Max Heiber
  • 14,346
  • 12
  • 59
  • 97
  • Note: this is not the same issue as https://stackoverflow.com/questions/50593774/cannot-find-typescript-module-even-though-tsc-successfully-manages-to-resolve-it, which was about a runtime failure. – Max Heiber May 01 '19 at 10:52

1 Answers1

-1

The cause of this issue is an incorrect named path in the paths option in tsconfig.json. You are defining a path named foo to point to "../typings/foo" relative to the project's root, but that is not correct because as it translates to a non-existent path:

Assuming the proj directory is this:

C:/dev/ts-options

then combined with the defined foo path it expands to:

C:/dev/ts-options/../typings/foo

which results in the non-existent path:

C:/dev/rp-build/node/rp-cli/test/fixtures/src/typescript/typings/foo.

The resolving does find it eventually by going down all the directories but the import uses the foo named path that gets translated to the above wrong full path.

So there are two solutions, either you correct the foo path definition in the tsconfig.json, or use a relative path to the main.ts of foo/index.d.ts in the import statement like this:

import foo from "../../../whatever/typings/foo"
Max Heiber
  • 14,346
  • 12
  • 59
  • 97
J. Koutsoumpas
  • 857
  • 4
  • 7
  • 1
    Thanks for this answer, but it doesn't seem to be correct or fully answer what I was asking. The path resolution is correct: you may have missed the part of my question where I said where I'm running 'tsc' (edited the question to make this clearer). Also, this answer doesn't address 'Question 1' or 'Question 2', which were what I wanted to know. – Max Heiber May 01 '19 at 15:25
  • It is correct for your question, before you edited it and changed it. Also why did you edit my answer? – J. Koutsoumpas Dec 21 '20 at 19:45
  • 1
    I'm sorry, I have no memory of this and am not working on this stuff anymore. I'll vote your answer up – Max Heiber Dec 21 '20 at 21:50