8

On compile i get error that i can't fix :/

index.ts:2:19 - error TS2307: Cannot find module '@shared'

Any ideas why is it?

Project structure:

enter image description here

tsconfig.json:

{
  "compilerOptions": {
    "outDir": "../../build/backend",
  },
  "extends": "../../configs/tsconfig.base.json",
  "references": [
    {
      "path": "../shared"
    }
  ],
  "paths": {
    "@shared": [
      "../shared/index"
    ]
  }
}

backend/index.ts:

import 'module-alias/register'
import { x } from '@shared'

console.log(x)

shared/index.ts:

export const x = 'this is shared index'
ZiiMakc
  • 31,187
  • 24
  • 65
  • 105

2 Answers2

13

You can use this command to trace problems:

tsc --traceResolution

As i found out '@shared' is used as folder name, so it's looks like:

Resolving module name '@shared' relative to base url 'D:/apps/my-app/src' - 'D:/apps/my-app/src/@shared'.

After i changed alias to 'shared' and set baseUrl everything starts to work:

{
  "compilerOptions": {
    "moduleResolution": "node",
    "baseUrl": "../",
    "outDir": "../../build/backend"
  },
  "extends": "../../configs/tsconfig.base.json",
  "references": [
    {
      "path": "../shared"
    }
  ],
  "paths": { "shared": ["shared/index"] }
}
ZiiMakc
  • 31,187
  • 24
  • 65
  • 105
  • Hi there. Im having a similar problem but this doesn't seem to help. Do you think you'd be able to check my question out also please if you have time? Thanks. https://stackoverflow.com/questions/67281799/typescript-aliases-not-working-properly-when-i-init-dependency – TheMan68 Apr 27 '21 at 19:54
7

OMG I figure it out finally. The baseUrl and paths should be inside the compilerOptions and not outside!

Orel Hassid
  • 301
  • 5
  • 4
  • 4
    Embarrassed to say I had the same issue haha. Thanks for posting this, made me realize what I was doing wrong – Themis Aug 05 '22 at 07:35