Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find module - typescript path alias error

Tags:

typescript

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'
like image 515
ZiiMakc Avatar asked Feb 18 '26 06:02

ZiiMakc


2 Answers

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"] }
}
like image 103
ZiiMakc Avatar answered Feb 19 '26 20:02

ZiiMakc


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

like image 20
Orel Hassid Avatar answered Feb 19 '26 19:02

Orel Hassid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!