Ncc: nestjs + typeorm + ncc missing class-validator and class-transformer

Created on 16 Mar 2019  路  6Comments  路  Source: vercel/ncc

i run ncc build ./src/main.ts -o and run node index.js.

display these tips.

[PackageLoader] The "class-validator" package is missing. Please, make sure to install this library ($ npm install class-validator) to take advantage of ValidationPipe. +4ms

and

[PackageLoader]  The "class-transformer" package is missing. Please, make sure to install this library ($ npm install class-transformer) to take advantage of ValidationPipe. +18ms

and then i run yarn init && yarn add class-validator yarn add class-transformer.

it works. 馃憤

but if there is a better way to solve it so that i don't need to use yarn add?


btw. i want to thxs @dirkdev98 in https://github.com/zeit/ncc/issues/245 's solution. help me fixed typeorm error.

Most helpful comment

BTW,

if your use ncc build nest.js 6.0.0, see this error [PackageLoader] No driver (HTTP) has been selected. In order to take advantage of the default driver, please, ensure to install the "@nestjs/platform-express" package ($ npm install @nestjs/platform-express).

you can try declare type use express.


before:

const app = await NestFactory.create(AppModule);

after:

const server = express();
const app = await NestFactory.create(AppModule, new ExpressAdapter(server));

All 6 comments

My guess is that class-validator and class-transformer are optional dependencies.

Can you post the contents of package.json and main.ts?

hi @styfle , here is these file code. class-validator and class-transformer in dependencies.

package.json

"dependencies": {
    "@nestjs/common": "5.7.4",
    "@nestjs/core": "5.7.4",
    "@nestjs/jwt": "^0.3.0",
    "@nestjs/microservices": "5.7.4",
    "@nestjs/passport": "^5.1.0",
    "@nestjs/typeorm": "5.3.0",
    "@types/passport-jwt": "^3.0.1",
    "app-root-path": "^2.1.0",
    "bcryptjs": "^2.4.3",
    "chokidar": "^2.1.2",
    "class-transformer": "^0.2.0",
    "class-validator": "^0.9.1",
    "express": "^4.16.4",
    "moment": "^2.24.0",
    "mysql": "^2.16.0",
    "passport": "^0.4.0",
    "passport-jwt": "^4.0.0",
    "reflect-metadata": "0.1.13",
    "request-ip": "^2.1.3",
    "rimraf": "2.6.3",
    "rxjs": "6.4.0",
    "typeorm": "0.2.15",
    "typescript": "3.3.3333"
  },
  "devDependencies": {
    "@nestjs/testing": "5.7.4",
    "@types/app-root-path": "^1.2.4",
    "@types/bcryptjs": "^2.4.2",
    "@types/express": "4.16.1",
    "@types/jest": "24.0.11",
    "@types/node": "11.11.3",
    "@types/request-ip": "^0.0.33",
    "@types/supertest": "^2.0.5",
    "@typescript-eslint/eslint-plugin": "^1.4.2",
    "@typescript-eslint/parser": "^1.4.2",
    "eslint": "5.15.1",
    "eslint-config-airbnb": "17.1.0",
    "eslint-config-airbnb-typescript": "^1.1.0",
    "eslint-loader": "2.1.2",
    "eslint-plugin-css-modules": "2.11.0",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-jsx-a11y": "^6.1.1",
    "eslint-plugin-react": "^7.11.0",
    "eslint-plugin-typescript": "0.14.0",
    "jest": "24.5.0",
    "nodemon": "1.18.10",
    "prettier": "1.16.4",
    "supertest": "4.0.2",
    "ts-jest": "24.0.0",
    "ts-node": "8.0.3",
    "ts-node-dev": "^1.0.0-pre.30"
  }

main.ts

import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import appRoot from 'app-root-path';

import { envConfig } from './configs';
import { AppModule } from './app.module';
import { HttpExceptionFilter } from './filters';


async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  const port = process.env.PORT || envConfig.PORT;

  app.useStaticAssets(appRoot.path, {
    prefix: '/public',
  });

  app.enableCors({
    origin: true,
    credentials: true,
    maxAge: 0,
    optionsSuccessStatus: 200,
    exposedHeaders: ['Authorization'],
  });

  app.useGlobalPipes(new ValidationPipe({
    transform: true,
    whitelist: true,
    forbidNonWhitelisted: true,
    skipMissingProperties: false,
    forbidUnknownValues: true,
  }));

  app.useGlobalFilters(
    new HttpExceptionFilter(),
  );

  const serverInfoByText = `${envConfig.MODE}-${envConfig.PROTOCOL.toUpperCase()}-SERVER --> ${envConfig.BASE_URL}:${envConfig.PORT}`;
  const serverInfoByEmoji = `\n鉁ㄢ湪 \x1b[30;42;1m ${serverInfoByText} \x1b[0m 鉁ㄢ湪\n\n`;

  await app.listen(port, () => {
    console.info(serverInfoByEmoji);
  });
}

(async () => bootstrap())();

hi, @styfle.

today I update nest.js to @nestjs/common": "6.0.0"锛宎ll errors disappeared. 馃槅

BTW,

if your use ncc build nest.js 6.0.0, see this error [PackageLoader] No driver (HTTP) has been selected. In order to take advantage of the default driver, please, ensure to install the "@nestjs/platform-express" package ($ npm install @nestjs/platform-express).

you can try declare type use express.


before:

const app = await NestFactory.create(AppModule);

after:

const server = express();
const app = await NestFactory.create(AppModule, new ExpressAdapter(server));

My guess is that class-validator and class-transformer are optional dependencies.
Can you post the contents of package.json and main.ts?

@styfle when packages are listed in the dependencies section of the package.json file, shouldn't they always be included by ncc?
Sorry if this is a stupid noop question, but I failed to find any ncc-docs that explain how the dependencies are handled.

when packages are listed in the dependencies section of the package.json file, shouldn't they always be included by ncc?

No, the purpose of ncc is to remove unnecessary code. For example, the dependency might be in package.json but if you never require() it then it won't be included in the bundled output.

Sometimes ncc misses dynamic code which why we still have issues reported here 馃槃

Was this page helpful?
0 / 5 - 0 ratings

Related issues

coetry picture coetry  路  5Comments

j picture j  路  3Comments

maku picture maku  路  3Comments

rgbkrk picture rgbkrk  路  3Comments

stevage picture stevage  路  4Comments