Nestjs 错误:找不到模块“./app.controller”
Posted
技术标签:
【中文标题】Nestjs 错误:找不到模块“./app.controller”【英文标题】:Nestjs Error: Cannot find module './app.controller' 【发布时间】:2020-08-02 00:04:46 【问题描述】:我似乎无法找到错误的来源,因为应用程序使用Found 0 errors. Watching for file changes.
编译我在 *** 上看到了类似的解决方案,但似乎没有一个解决问题
这是堆栈跟踪
internal/modules/cjs/loader.js:797
throw err;
^
Error: Cannot find module './app.controller'
Require stack:
- C:\Users\DELL\Documents\DokunFiles\Nestjs\app\api\dist\src\app.module.js
- C:\Users\DELL\Documents\DokunFiles\Nestjs\app\api\dist\src\main.js
通过下面的appModule,应用控制器被正确导入到应用模块中
app.module.ts
import Module from '@nestjs/common';
import MongooseModule from '@nestjs/mongoose';
import AppController from './app.controller';
import AppService from './app.service';
import UserModule from './user/user.module';
@Module(
imports: [MongooseModule.forRoot(process.env.MONGO_URI,
useNewUrlParser: true,
useUnifiedTopology: true
)
, UserModule,
],
controllers: [AppController],
providers: [AppService],
)
export class AppModule
app.controller.ts
import Controller, Get from '@nestjs/common';
import AppService from './app.service';
@Controller()
export class AppController
constructor(private readonly appService: AppService)
@Get()
getHello(): string
return this.appService.getHello();
【问题讨论】:
这很奇怪,我遇到了同样的错误。很高兴我不是唯一一个。我注意到控制器没有在 dist/src 文件夹中编译。 我遇到了同样的问题。运行prebuild
也为我解决了这个问题。也许是一个错误?
【参考方案1】:
在您的 tsconfig.json
文件中查找 "incremental": true
。 true 进行增量更新。希望将其设置为 false 可以让您不必每次都清理 dist
【讨论】:
所以增量会节省本地开发应用程序的时间,因为它编译速度会更快?顺便提一句。这适用于我的nestjs应用程序中的类似错误:D【参考方案2】:运行:
npm run prebuild
或
rimraf dist
或
rm -rf dist/
然后再试一次。
我遇到了同样的问题:
stanislas@yeji > nest start api -> master ! ? RC=130
internal/modules/cjs/loader.js:983
throw err;
^
Error: Cannot find module './app.controller'
Require stack:
- /Users/stanislas/git/soundbase/api/dist/src/app.module.js
- /Users/stanislas/git/soundbase/api/dist/src/main.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15)
at Function.Module._load (internal/modules/cjs/loader.js:862:27)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/Users/stanislas/git/soundbase/api/dist/src/app.module.js:13:26)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Module.require (internal/modules/cjs/loader.js:1042:19)
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/stanislas/git/soundbase/api/dist/src/app.module.js',
'/Users/stanislas/git/soundbase/api/dist/src/main.js'
]
并注意到控制器确实没有编译:
stanislas@yeji > ll dist/src/ api -> master ! ?
total 48
-rw-r--r-- 1 stanislas staff 138B Apr 19 17:45 app.module.d.ts
-rw-r--r-- 1 stanislas staff 2.1K Apr 19 17:45 app.module.js
-rw-r--r-- 1 stanislas staff 753B Apr 19 17:45 app.module.js.map
-rw-r--r-- 1 stanislas staff 11B Apr 19 17:45 main.d.ts
-rw-r--r-- 1 stanislas staff 340B Apr 19 17:45 main.js
-rw-r--r-- 1 stanislas staff 290B Apr 19 17:45 main.js.map
drwxr-xr-x 5 stanislas staff 160B Apr 19 17:52 migration
但我注意到它是 prod
:
stanislas@yeji > npm run build && npm run start:prod api -> master ! ? RC=1
> soundbase-api@0.0.1 prebuild /Users/stanislas/git/soundbase/api
> rimraf dist
> soundbase-api@0.0.1 build /Users/stanislas/git/soundbase/api
> nest build
> soundbase-api@0.0.1 start:prod /Users/stanislas/git/soundbase/api
> node dist/main
[Nest] 95175 - 04/19/2020, 6:40:45 PM [NestFactory] Starting Nest application...
[Nest] 95175 - 04/19/2020, 6:40:45 PM [InstanceLoader] PassportModule dependencies initialized +34ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [InstanceLoader] JwtModule dependencies initialized +1ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [InstanceLoader] AuthModule dependencies initialized +0ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [InstanceLoader] AppModule dependencies initialized +1ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [InstanceLoader] TypeOrmCoreModule dependencies initialized +56ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [InstanceLoader] UsersModule dependencies initialized +0ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [RoutesResolver] AppController : +3ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [RouterExplorer] Mapped /auth/login, POST route +3ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [RouterExplorer] Mapped /profile, GET route +0ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [RoutesResolver] UserController /users: +0ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [RouterExplorer] Mapped /users, POST route +1ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [RouterExplorer] Mapped /users, GET route +0ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [RouterExplorer] Mapped /users/:id, GET route +1ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [RouterExplorer] Mapped /users/:id, PUT route +0ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [RouterExplorer] Mapped /users/:id, DELETE route +0ms
[Nest] 95175 - 04/19/2020, 6:40:45 PM [NestApplication] Nest application successfully started +2ms
然后它又自动开始工作了:
stanislas@yeji > npm start api -> master ! ? RC=130
> soundbase-api@0.0.1 start /Users/stanislas/git/soundbase/api
> nest start
[Nest] 95255 - 04/19/2020, 6:41:00 PM [NestFactory] Starting Nest application...
[Nest] 95255 - 04/19/2020, 6:41:00 PM [InstanceLoader] PassportModule dependencies initialized +35ms
[Nest] 95255 - 04/19/2020, 6:41:00 PM [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 95255 - 04/19/2020, 6:41:00 PM [InstanceLoader] JwtModule dependencies initialized +0ms
[Nest] 95255 - 04/19/2020, 6:41:00 PM [InstanceLoader] AuthModule dependencies initialized +1ms
[Nest] 95255 - 04/19/2020, 6:41:00 PM [InstanceLoader] AppModule dependencies initialized +0ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [InstanceLoader] TypeOrmCoreModule dependencies initialized +56ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [InstanceLoader] UsersModule dependencies initialized +1ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [RoutesResolver] AppController : +3ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [RouterExplorer] Mapped /auth/login, POST route +2ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [RouterExplorer] Mapped /profile, GET route +1ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [RoutesResolver] UserController /users: +0ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [RouterExplorer] Mapped /users, POST route +1ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [RouterExplorer] Mapped /users, GET route +0ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [RouterExplorer] Mapped /users/:id, GET route +0ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [RouterExplorer] Mapped /users/:id, PUT route +1ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [RouterExplorer] Mapped /users/:id, DELETE route +0ms
[Nest] 95255 - 04/19/2020, 6:41:01 PM [NestApplication] Nest application successfully started +2ms
由于我事先已经尝试过nest build && node dist/main.js
,我很确定npm run prebuild
又名rimraf dist
(只是rm -rf dist/
)成功了。由于问题出在dist
文件夹中,这对我来说似乎很合乎逻辑。不知道确切的问题是什么。
【讨论】:
我按照你的建议做了,所以我得到了Nest can't resolve dependencies of the UserService (?). Please make sure that the argument UserModel at index [0] is available in the UserModule context. Potential solutions: - If UserModel is a provider, is it part of the current UserModule? - If UserModel is exported from a separate @Module, is that module imported within UserModule? @Module( imports: [ /* the Module containing UserModel */ ] )
它正在工作。运行命令后,我收到了上述错误,我在 *** ***.com/questions/56870498/… 上引用了此链接。谢谢大佬以上是关于Nestjs 错误:找不到模块“./app.controller”的主要内容,如果未能解决你的问题,请参考以下文章
Nestjs错误。找不到模块'.app.controller'。
带有nestjs的Angular-universal:错误:找不到模块'./drivers/node-mongodb-native/connection'
NestJS - 测试套件无法运行从“comment/comment.entity.ts”中找不到模块“src/article/article.entity”