NestJS microservices "Cannot find module"

8.4k Views Asked by At

So, I'm trying to create my first microservice using NestJS, but the moment I try to run it, the service stops with this error:

[13:39:21] Found 0 errors. Watching for file changes.

Error: Cannot find module 'C:\Users\voryi\IdeaProjects\YWA\des_server\services\learning-service\dist\main'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47
5

There are 5 best solutions below

1
Micael Levi On BEST ANSWER

there's no main.js file in the first level of your dist directory. You can define the entry file by adding this to your nest-cli.json:
"entryFile": "learning-service/src/main"
the default is main

2
Hai Alison On

try npm run build and then restart your service

0
RicardoVallejo On

In my case, the main.js file was inside src folder so I had to change the script to:

"start:prod": "node dist/src/main"
0
ganesshkumar On

I use monorepo setup and this issue happens whenever I start the nestjs server from non-root folder by mistake.

Usually, removing the dist folder and restarting the nestjs server works for me.

0
Nijoo On

maybe simply use this inside the project,

npm i @nestjs/microservices