nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.36k stars 7.59k forks source link

Build fails with the message, 'Error The "@nestjs/swagger" plugin is not compatible with Nest CLI. Neither "after()" nor "before()" nor "afterDeclarations()" function have been provided. #10694

Closed DBTK1990 closed 1 year ago

DBTK1990 commented 1 year ago

Is there an existing issue for this?

Current behavior

get an error when trying to build nest app with swagger on it as a plugin but first you need to omit dev-dependencies i saw issue https://github.com/nestjs/nest/issues/9650 and its pretty much the same

his solution was to change the node env to development from production but when we set it to production, the default behavior for npm is to not install dev-dependencies and when we set it to something other then that the dev-dependencies will be install by npm

this is a bug on your part, please fix :)

Minimum reproduction code

~

Steps to reproduce

  1. create nest app
  2. add @nestjs/swagger to package.json witn npm i @nestjs/swagger
  3. configre nest-cli.json to use @nestjs/swagger plugin
  4. run npm install --omit=dev // remove dev dependencies from node_modules
  5. run npx @nestjs/cli start

Expected behavior

build succeed and site is served

Package

Other package

@nestjs/swagger

NestJS version

9.0.3

Packages versions

{
  "name": "conf-0.0.1.19",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "scripts": {
    "start": "npx -y @nestjs/cli start  --tsc --path ./tsconfig.json",
    "start:docs": "npx -y @compodoc/compodoc -p tsconfig.json -s -r 8082",
    "start:dev": "npx -y @nestjs/cli start --watch --tsc --debug 0.0.0.0:51234 --path ./tsconfig.json",
    "start:cloud-env": "npx -y @nestjs/cli start  --tsc --path ./tsconfig.build.json",
    "docker:database": "docker-compose -p self-schedule-db -f docker/docker-compose.database.yml up -d",
    "docker:main": "docker-compose -p self-schedule-main -f ./docker/docker-compose.base.yml -f ./docker/docker-compose.development.yml up",
    "docker:main:qa": "docker-compose -p self-schedule-main-qa -f ./docker/docker-compose.qa.yml up",
    "docker:dev": "npm run docker:database --force-recreate && npm run docker:main --force-recreate --",
    "docker:dev:qa": "npm run docker:database --force-recreate && npm run docker:main:qa --force-recreate --",
    "docker:test-explorer": "npm run docker:database && docker run --rm --network self-schedule-db_db-network -v $INIT_CWD:/home/node/server -v $HOME/.vscode-server/extensions:$HOME/.vscode-server/extensions -v $INIT_CWD/docker/db.env:/home/node/server/.env self-schedule-main_server npm run test --",
    "test": "DOTENV_CONFIG_PATH=./docker/test.env jest --setupFiles dotenv/config --bail",
    "test:watch": "npx -y jest  --watch",
    "test:cov": "npm t -- --coverage",
    "test:debug": "npx -y ts-node-dev --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "npm t -- --config ./test/jest-e2e.json",
    "lint": "npx -y eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "format": "npx -y prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "docs": "npx -y @compodoc/compodoc -p tsconfig.json -s -r 8082",
    "db:migrate:dev": "npx -y prisma migrate dev",
    "db:migrate": "npx -y prisma migrate deploy",
    "db:generate": "npx -y prisma generate",
    "db:seed": "npx -y prisma db seed",
    "db:reset": "npx -y prisma migrate reset"
  },
  "dependencies": {
    "@aws-sdk/client-glue": "^3.110.0",
    "@chasdevs/avro-to-typescript": "^1.4.0",
    "@fastify/static": "^6.4.0",
    "@nestjs/axios": "^0.1.0",
    "@nestjs/common": "^9.0.3",
    "@nestjs/config": "^2.2.0",
    "@nestjs/core": "^9.0.3",
    "@nestjs/event-emitter": "^1.3.0",
    "@nestjs/mapped-types": "^1.1.0",
    "@nestjs/microservices": "^9.0.3",
    "@nestjs/platform-fastify": "^9.0.3",
    "@nestjs/schedule": "^2.1.0",
    "@nestjs/swagger": "^6.0.3",
    "@prisma/client": "^3.11.1",
    "avsc": "^5.7.4",
    "cache-manager": "^4.0.1",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2",
    "dayjs": "^1.11.2",
    "kafkajs": "^2.0.2",
    "lodash": "^4.17.21",
    "morgan": "^1.10.0",
    "morgan-json": "^1.1.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0",
    "sql-formatter": "^8.2.0",
    "winston": "^3.7.2",
    "@types/node": "^16.11.26"
  },
  "devDependencies": {
    "@compodoc/compodoc": "^1.1.19",
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/cache-manager": "^4.0.0",
    "@types/cron": "^2.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "27.4.1",
    "@types/lodash": "^4.14.182",
    "@types/morgan": "^1.9.3",
    "@types/morgan-json": "^1.1.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "prisma": "^3.11.1",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "^27.2.5",
    "jest-mock-extended": "^2.0.4",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "^27.0.3",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.7.0",
    "ts-node-dev": "^1.1.8",
    "tsconfig-paths": "^3.10.1",
    "typescript": "^4.6.3"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node",
    "moduleNameMapper": {
      "^@/(.*)$": "<rootDir>/../src/$1"
    }
  },
  "prisma": {
    "seed": "npx -y ts-node prisma/seed.ts"
  }
}

Node.js version

16.14

In which operating systems have you tested?

Other

No response

micalevisk commented 1 year ago

I don't get why you're trying to use NestJS's CLI without having dev dependencies installed.

Instead, you should do this:

npm i # install all deps
npm run build
npm install --omit=dev
npm run start:prod
Tony133 commented 1 year ago

However, by doing some tests, the command that causes the error is the following

npm install --omit=dev

following the procedure described by @DBTK1990.

in fact not using npm install --omit=dev the procedure works correctly. (see screenshot) in a local or dev environment

screenshot

also using the procedure listed by @micalevisk, the error is not generated ( see screenshot )

screenshot-two

in conclusion it occurs to me that the error is generated by an incorrect command procedure, there is an issue like this open in the nest-cli repository see here, waiting for a minimal reproduction, most likely I think it's a very similar problem to this.

I hope I was helpful

DBTK1990 commented 1 year ago

hey guys, i know that if we don't use npm install --omit=dev it will work. currently we fixed it by using the npm install without the omit but i can think on a couple of usages for this.

@micalevisk

I don't get why you're trying to use NestJS's CLI without having dev dependencies installed.

Instead, you should do this:

npm i # install all deps
npm run build
npm install --omit=dev
npm run start:prod

but if we do this we cannot use nest start command because this command creates a different build process and the nest start is my npm prod (can see it in the issue) command because we need the swagger site on some environments (QA,DEV,STAGE)

@Tony133 do you think i need to move the issue to nestjs/cli repo? if there is another way to run the swagger outside of nest start i will be glad to know. because when we use the command of nest build it does not create the a build for swagger too.

Tony133 commented 1 year ago

@Tony133 pensi che debba spostare il problema nel repository nestjs/cli?

Yes, maybe it was to be opened in the nestjs-cli repository, but anyway now that it has been opened in this repository by now better not to create another issue in another repository, at most the NestJS core team will move it directly to the repository where they think it is most suitable after an evaluation.

if there is another way to run the swagger outside of nest start i will be glad to know.

For swagger, if you are looking for a plugin alternative, you can manually declare all decorators or only specific decorators where you need them, see here. Currently, I have always used the @nestjs/swagger package to manage swagger with NestJS.

micalevisk commented 1 year ago

If you're using the start command just because of the swagger plugin, I guess you could try this: https://docs.nestjs.com/openapi/cli-plugin#integration-with-ts-jest-e2e-tests

kamilmysliwiec commented 1 year ago

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.