Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix: Cannot find module '@entities/user' with custom paths?

Tags:

nestjs

I would like to use custom paths from Typescript.

In my project, I switched from:

import { User } from 'src/entities/user.entity';

to:

import { User } from '@entities/user';

Into tsconfig.json I put :

"paths": {
  "@entities/*": ["src/entities/*.entity.ts"]
}

When I run npm run start:dev (=nodemon), I have this error: Error: Cannot find module '@entities/user'

In debug mode, npm prints out:

looking for "@entities/user" in [".../node_modules", ".../node_modules"]

It is never looking for in the folder "src/entities" or something similar.

Maybe it is because thoses paths are not interpreted when running ? Should I create some kind of bootstrap like here ?

FIX

The issue came from nodemon.json:

I had to replace:

{
  "watch": ["dist"],
  "ext": "js",
  "exec": "node dist/main"
}

By:

{
  "watch": ["src"],
  "ext": "ts",
  "exec": "ts-node -r tsconfig-paths/register src/main.ts"
}

Now TypeORM knows where to find the entities in development env.

like image 829
Quentin Lapointe Avatar asked Sep 02 '25 02:09

Quentin Lapointe


1 Answers

FIX

The issue came from nodemon.json:

I had to replace:

{
  "watch": ["dist"],
  "ext": "js",
  "exec": "node dist/main"
}

By:

{
  "watch": ["src"],
  "ext": "ts",
  "exec": "ts-node -r tsconfig-paths/register src/main.ts"
}

Now TypeORM knows where to find the entities in development env.

like image 104
Quentin Lapointe Avatar answered Sep 05 '25 01:09

Quentin Lapointe