简体   繁体   English

NestJS 微服务“找不到模块”

[英]NestJS microservices "Cannot find module"

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:因此,我正在尝试使用 NestJS 创建我的第一个微服务,但当我尝试运行它时,服务因以下错误而停止:

[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

try npm run build and then restart your service尝试npm run build然后重启你的服务

there's no main.js file in the first level of your dist directory. dist目录的第一级中没有main.js文件。 You can define the entry file by adding this to your nest-cli.json :您可以通过将其添加到您的nest-cli.json来定义入口文件:
"entryFile": "learning-service/src/main"
the default is main默认是main

In my case, the main.js file was inside src folder so I had to change the script to:就我而言,main.js 文件位于 src 文件夹中,因此我必须将脚本更改为:

"start:prod": "node dist/src/main"

I use monorepo setup and this issue happens whenever I start the nestjs server from non-root folder by mistake.我使用 monorepo 设置,每当我错误地从非根文件夹启动 nestjs 服务器时,就会发生此问题。

Usually, removing the dist folder and restarting the nestjs server works for me.通常,删除 dist 文件夹并重新启动 nestjs 服务器对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM