nestjs / swagger

OpenAPI (Swagger) module for Nest framework (node.js) :earth_americas:
https://nestjs.com
MIT License
1.68k stars 464 forks source link

cannot find module error #1583

Closed hongdor closed 2 years ago

hongdor commented 2 years ago

I'm submitting a...


[ ] Regression 
[v] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I think there is a problem with referring to the src dir. but The build foler is dist dir.

npm run start:dev => OK npm run start / npm run start:prod => Fail

I use nestJs hot-reload function It was normal before the openapi(swagger) cli plugin of nestjs was applied.

All config setting followed the official document of the nestjs


C:\Users\Hong\Desktop\restapi_개발\nodeJs\test_v2_API>npm run start

> nest-typescript-starter@1.0.0 start C:\Users\Hong\Desktop\restapi_개발\nodeJs\test_v2_API
> nest start
Error: Cannot find module 'C:/Users/Hong/Desktop/restapi_개발/nodeJs/test_v2_API/src/farm/entities/farm.entity'Require stack:
- C:\Users\Hong\Desktop\restapi_개발\nodeJs\test_v2_API\dist\farm\farm.controller.js
- C:\Users\Hong\Desktop\restapi_개발\nodeJs\test_v2_API\dist\farm\farm.module.js
- C:\Users\Hong\Desktop\restapi_개발\nodeJs\test_v2_API\dist\app.module.js
- C:\Users\Hong\Desktop\restapi_개발\nodeJs\test_v2_API\dist\main.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
    at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object. (C:\Users\Hong\Desktop\restapi_개발\nodeJs\test_v2_API\dist\farm\farm.controller.js:51:47)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! nest-typescript-starter@1.0.0 start: `nest start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the nest-typescript-starter@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Hong\AppData\Roaming\npm-cache\_logs\2021-10-01T12_48_53_493Z-debug.log

Expected behavior

Minimal reproduction of the problem with instructions

farm.controller.ts


import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { FarmService } from './farm.service';
import { CreateFarmDto } from './dto/create-farm.dto';
import { UpdateFarmDto } from './dto/update-farm.dto';
import { Farm } from './entities/farm.entity';

@Controller('api/v2/farms/test')
export class FarmController {
  constructor(private readonly farmService: FarmService) {}

  @Post()
  create(@Body() createFarmDto: CreateFarmDto) {
    return this.farmService.create(createFarmDto);
  }

  @Get()
  findAll(): Promise {
    return this.farmService.findAll();
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.farmService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateFarmDto: UpdateFarmDto) {
    return this.farmService.update(+id, updateFarmDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.farmService.remove(+id);
  }
}

What is the motivation / use case for changing the behavior?

Environment


Nest version: 8.0.6


For Tooling issues:
- Node version: 14.15.5  
- Platform:  Windows 

Others:
  "dependencies": {
    "@nestjs/common": "^8.0.6",
    "@nestjs/core": "^8.0.6",
    "@nestjs/mapped-types": "*",
    "@nestjs/platform-express": "^8.0.6",
    "@nestjs/swagger": "^5.1.0",
    "@nestjs/typeorm": "^8.0.2",
    "keycloak-connect": "15.0.2",
    "mysql": "^2.18.1",
    "mysql2": "^2.3.0",
    "nest-keycloak-connect": "^1.7.6",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.3.0",
    "swagger-ui-express": "^4.1.6",
    "typeorm": "^0.2.37",
    "typeorm-naming-strategies": "^2.0.0"
  }
  "devDependencies": {
    "@nestjs/cli": "^8.1.1",
    "@nestjs/schematics": "^8.0.2",
    "@nestjs/testing": "^8.0.6",
    "@types/express": "^4.17.13",
    "@types/jest": "^27.0.1",
    "@types/node": "^16.7.1",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^4.29.2",
    "@typescript-eslint/parser": "^4.29.2",
    "eslint": "^7.32.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^3.4.1",
    "jest": "^27.0.6",
    "prettier": "^2.3.2",
    "run-script-webpack-plugin": "0.0.11",
    "supertest": "^6.1.6",
    "ts-jest": "^27.0.5",
    "ts-loader": "^9.2.5",
    "ts-node": "^10.2.1",
    "tsconfig-paths": "^3.10.1",
    "typescript": "^4.3.5",
    "webpack": "^5.53.0",
    "webpack-cli": "^4.8.0",
    "webpack-node-externals": "^3.0.0"
  }

micalevisk commented 2 years ago

Could you please provide a minimum reproduction repository for this? It will help the core team and folks like me to understand your issue faster.

hongdor commented 2 years ago

Could you please provide a minimum reproduction repository for this? It will help the core team and folks like me to understand your issue faster.

I'm sorry. this is my code (deleted)

hongdor commented 2 years ago

I solved it

I added <"webpack" : true > in the nest-cli.json file

(you can check the contents in the document below https://docs.nestjs.com/cli/monorepo#cli-properties)


{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {    
    "webpack": true,
    "plugins": [
      {
        "name": "@nestjs/swagger",
        "options": {
          "classValidatorShim": false,
          "introspectComments": true
        }
      }
    ]
  }
}