ZiiMakc
ZiiMakc

Reputation: 37066

Cannot find module - typescript path alias error

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'

Upvotes: 11

Views: 38292

Answers (2)

Orel Hassid
Orel Hassid

Reputation: 390

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

Upvotes: 14

ZiiMakc
ZiiMakc

Reputation: 37066

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"] }
}

Upvotes: 15

Related Questions