6

Here's the Github MCVE showing an issue. npm run compile shows the error.

I'm trying to do this:

import {Todo} from '@test';

But it's not resolving.

src/index.ts:1:20 - error TS2307: Cannot find module '@test'.

I have paths in tsconfig.json.

  "baseUrl": "./",                          /* Base directory to resolve non-absolute module names. */
  "paths": {
    "@fs/": ["src/"], 
    "@test/": ["test/"]
  },                                        /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */

Thoughts?

Github Typescript Issue

I opened a bug report for this here since per some of the comments it should have worked.

Typescript note that I updated the github repository removing the glob patterns and slashes in @test.

Ole
  • 41,793
  • 59
  • 191
  • 359
  • [In the docs](https://www.typescriptlang.org/docs/handbook/module-resolution.html) under path mapping they have set the base url to just `.` – pmkro Mar 27 '19 at 18:18
  • @pmkro `.` and `./` resolve to the same directory, the contrary would be a TS implementation bug – Nino Filiu Mar 27 '19 at 18:20
  • @NinoFiliu I figured as much – pmkro Mar 27 '19 at 18:21
  • For those who are not using a bundler (CRA, webpack, Nextjs, etc) and are stuck with this problem, maybe this will help you: https://stackoverflow.com/a/73346203/8221175 – Victor Aug 13 '22 at 17:07

2 Answers2

7

Don't adjust the VS Code Import Module Specifier settings (as per some answers floating around). And don't remove the glob patterns. In fact add some more in:

 "baseUrl": "./",
  "paths": {
    "@fs/*": ["src/*"], 
    "@test/*": ["test/*"]
  },

Notice that the glob is in the key AND the value. This is hilariously hard to spot sometimes.

Where the target is a file, the glob should be omitted from key and value.

Jai
  • 2,768
  • 24
  • 20
2

Remove the glob patterns:

"paths": {
  "@test": "./test"
}

Your solution is weirdly supposed to work, but the docs use no globs and there have been some reported intellisense bugs when using globs.

Nino Filiu
  • 16,660
  • 11
  • 54
  • 84
  • Well, yes, and also [this article](https://netbasal.com/sexier-imports-in-typescript-e3c645bdd3c6) says the same. But on my machine, removing the glob pattern made the whole thing work as expected, and adding them back provoked a bug. That's enough to be an answer for me. – Nino Filiu Mar 27 '19 at 18:30
  • IMO there is definitely a bug in somewhere between my IDE and TS' implementation, but still, if a fix works for someone, it has value as an answer – Nino Filiu Mar 27 '19 at 18:31
  • I tried removing the glob pattern and it still does not resolve for me. I pushed the changes to the github repository. – Ole Mar 27 '19 at 18:40
  • Also the issue is separate from VSCode. I'm running `npm run compile` configured in the `package.json` and the `src/index.ts:1:20 - error TS2307: Cannot find module '@test'` ` is still logged. – Ole Mar 27 '19 at 18:53
  • You didn't say I wrote ;) it's `"@test": ["./test"]`, not `"@test/": ["./test/"]`. No slash. – Nino Filiu Mar 27 '19 at 18:58