nestjs / ng-universal

Angular Universal module for Nest framework (node.js) 🌷
https://nestjs.com
MIT License
442 stars 68 forks source link

Using this with the Mongoose module #115

Closed robstadnick closed 5 years ago

robstadnick commented 5 years ago

I'm submitting a...


[ ] Regression 
[ x] Bug report
[ ] Feature request
[ ] Documentation issue or request

Current behavior

I was attempting to connect a MongoDB instance and had an interesting time tracking down exactly why my app wasn't starting. As soon as I removed the AngularUniversalModule from the AppModule imports... my app started right up.

I spent the better part of the day tracking down what the issue might be to no solution. I attempted to make the AngularUniversalModule Dynamic and just tossed an await on a timer, however it stops all imports until the timer is over. So I'm obviously not quite as familiar with how this framework works. Maybe I just missed something?

Expected behavior

It seems like this Module needs to load after the Mongo Connection?

Minimal reproduction of the problem with instructions

https://github.com/robstadnick/nest-mongoose (edit: created new repo) There is a TODO on the app.module.ts above the code to comment out to view issue. dev - npm run serve prod - npm run build && npm start

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

Using this module and the Mongoose module together. Maybe there is a round about solution but it would be great to pass something into the forRoot() or have it instinctively load last?

Environment


Nest version: 6.5.3
- nestjs/ng-universal: 2.0.0
- @nestjs/mongoose: 6.1.2


For Tooling issues:
- Node version: 10.16.0  
- Platform:  Mac 
Others:

kresdjan commented 5 years ago

Plus one.

robstadnick commented 5 years ago

I would like to be able to combine these two modules however if you want a temp solution I was able to get this to work from the Documentation

I say temp because I haven't tested, might be production ready? ^^ (Nope, doesn't have the server side redering for Direct URLs) in your main.ts/server.ts


import * as path from 'path';
const DIST_FOLDER = path.join(process.cwd(), 'dist');
const DIST_BROWSER_FOLDER = path.join(DIST_FOLDER, 'browser');

async function bootstrap() {
  const app = await NestFactory.create(ApplicationModule);
  app.setViewEngine('html');
  app.useStaticAssets(DIST_BROWSER_FOLDER);
  app.setBaseViewsDir(DIST_BROWSER_FOLDER);
  const port = process.env.PORT || '4081';
  await app.listen(port);
  console.log('Started Server on Port ', port);
}
bootstrap();
robstadnick commented 5 years ago

UPDATE ^^^

I created a branch here with an example of using the MongooseModule with out this Module. I feel like it's a hack job and missing a few things but lets me continue with development until this issue is resolved.

A Few Notes: You can't use the global prefix, add api/ to the controller (much like versioning) The controller example is in the the mongo folder

REPO

ccampetella-clovinn commented 5 years ago

Can not use Mongoose with this module. When I try to run the main.js after build for production I always got the following error:

` npm run build:ssr

ikea-app@0.1.0 build:ssr D:\Trabajo\Programas_Realizados\Ikea\ikea-app npm run build:client-and-server-bundles && npm run compile:server

ikea-app@0.1.0 build:client-and-server-bundles D:\Trabajo\Programas_Realizados\Ikea\ikea-app ng build --prod && ng run ikea-app:server:production

chunk {0} runtime-es2015.24b02acc1f369d9b9f37.js (runtime) 2.83 kB [entry] [rendered] chunk {1} main-es2015.15cbb8f6351478d3c38a.js (main) 511 kB [initial] [rendered] chunk {2} polyfills-es2015.fd917e7c3ed57f282ee5.js (polyfills) 64.3 kB [initial] [rendered] chunk {3} polyfills-es5-es2015.f24fb8cb85b392c952f0.js (polyfills-es5) 211 kB [initial] [rendered] chunk {4} styles.8b62d9d878d7a23bdef9.css (styles) 61 kB [initial] [rendered] Date: 2019-08-29T23:39:20.413Z - Hash: 276a59f193cd3a49a42b - Time: 32519ms Generating ES5 bundles for differential loading... ES5 bundle generation complete. Hash: dce10a173b6f00270669 Time: 6822ms Built at: 2019-08-29 20:39:33 Asset Size Chunks Chunk Names main.js 65.6 KiB main [emitted] main main.js.map 36 KiB main [emitted] main Entrypoint main = main.js main.js.map chunk {main} main.js, main.js.map (main) 54.6 KiB [entry] [rendered]

ikea-app@0.1.0 compile:server D:\Trabajo\Programas_Realizados\Ikea\ikea-app webpack --config webpack.server.config.js --progress --colors

Hash: 48102827dcea5384f02c Version: webpack 4.39.2 Time: 33159ms Built at: 2019-08-29 20:40:16 Asset Size Chunks Chunk Names server-app\app.module.d.ts 45 bytes [emitted] server-app\auth\auth.controller.d.ts 487 bytes [emitted] server-app\auth\auth.module.d.ts 38 bytes [emitted] server-app\auth\dto\log-in.dto.d.ts 79 bytes [emitted] server-app\auth\dto\register.dto.d.ts 113 bytes [emitted] server-app\auth\services\auth.service.d.ts 449 bytes [emitted] server-app\auth\services\jwt.strategy.d.ts 411 bytes [emitted] server-app\environments\environments.d.ts 384 bytes [emitted] server-app\main.d.ts 12 bytes [emitted] server-app\upload\class\multer-file.class.d.ts 294 bytes [emitted] server-app\upload\class\s3-response.class.d.ts 188 bytes [emitted] server-app\upload\class\upload.class.d.ts 408 bytes [emitted] server-app\upload\dto\upload-picture.dto.d.ts 138 bytes [emitted] server-app\upload\models\upload.model.d.ts 419 bytes [emitted] server-app\upload\multer-options.d.ts 62 bytes [emitted] server-app\upload\services\upload.service.d.ts 781 bytes [emitted] server-app\upload\upload.controller.d.ts 551 bytes [emitted] server-app\upload\upload.module.d.ts 40 bytes [emitted] server-app\user\clases\user.class.d.ts 148 bytes [emitted] server-app\user\dto\profile.dto.d.ts 67 bytes [emitted] server-app\user\models\user.model.d.ts 216 bytes [emitted] server-app\user\services\user.service.d.ts 642 bytes [emitted] server-app\user\user.controller.d.ts 411 bytes [emitted] server-app\user\user.module.d.ts 38 bytes [emitted] server.js 14.6 MiB 0 [emitted] server Entrypoint server = server.js [0] ./server/main.ts 2.29 KiB {0} [built] [1] external "@nestjs/core" 42 bytes {0} [built] [2] ./server/app.module.ts 2.84 KiB {0} [built] [3] external "@nestjs/common" 42 bytes {0} [built] [8] external "path" 42 bytes {0} [built] [47] external "buffer" 42 bytes {0} [built] [73] external "zlib" 42 bytes {0} [built] [881] ./server/auth/auth.module.ts 1.74 KiB {0} [built] [882] ./server/auth/auth.controller.ts 4.07 KiB {0} [built] [1010] ./server/user/services/user.service.ts 5.14 KiB {0} [built] [1110] ./server/auth/services/auth.service.ts 2.39 KiB {0} [built] [1143] ./server/user/user.module.ts 1.6 KiB {0} [built] [1177] ./server/environments/environments.ts 631 bytes {0} [built] [1178] ./server/upload/upload.module.ts 1.47 KiB {0} [built] [1960] external "require(\"./server/main\")" 42 bytes {0} [built]

WARNING in ./node_modules/fastify-plugin/index.js Module not found: Error: Can't resolve 'fastify/package.json' in 'D:\Trabajo\Programas_Realizados\Ikea\ikea-app\node_modules\fastify-plugin' @ ./node_modules/fastify-plugin/index.js @ ./node_modules/fastify-swagger/index.js @ ./node_modules/@nestjs/swagger/dist/swagger-module.js @ ./node_modules/@nestjs/swagger/dist/index.js @ ./node_modules/@nestjs/swagger/index.js @ ./server/main.ts

campe@ASUS-PC MINGW64 /d/Trabajo/Programas_Realizados/Ikea/ikea-app (master) $ npm run serve:ssr

ikea-app@0.1.0 serve:ssr D:\Trabajo\Programas_Realizados\Ikea\ikea-app node dist/server

D:\Trabajo\Programas_Realizados\Ikea\ikea-app\dist\server.js:163006 throw e; ^

Error: Cannot find module './drivers/node-mongodb-native/connection' at webpackEmptyContext (D:\Trabajo\Programas_Realizados\Ikea\ikea-app\dist\server.js:163004:10) at Object. (D:\Trabajo\Programas_Realizados\Ikea\ikea-app\dist\server.js:64914:44) at webpack_require (D:\Trabajo\Programas_Realizados\Ikea\ikea-app\dist\server.js:20:30) at Object. (D:\Trabajo\Programas_Realizados\Ikea\ikea-app\dist\server.js:64199:18) at webpack_require (D:\Trabajo\Programas_Realizados\Ikea\ikea-app\dist\server.js:20:30) at Object. (D:\Trabajo\Programas_Realizados\Ikea\ikea-app\dist\server.js:64084:18) at webpack_require (D:\Trabajo\Programas_Realizados\Ikea\ikea-app\dist\server.js:20:30) at Object. (D:\Trabajo\Programas_Realizados\Ikea\ikea-app\dist\server.js:64015:32) at webpack_require (D:\Trabajo\Programas_Realizados\Ikea\ikea-app\dist\server.js:20:30) at Object. (D:\Trabajo\Programas_Realizados\Ikea\ikea-app\dist\server.js:53597:10) at webpack_require (D:\Trabajo\Programas_Realizados\Ikea\ikea-app\dist\server.js:20:30) at Object. (D:\Trabajo\Programas_Realizados\Ikea\ikea-app\dist\server.js:53583:10) at webpack_require (D:\Trabajo\Programas_Realizados\Ikea\ikea-app\dist\server.js:20:30) at Object. (D:\Trabajo\Programas_Realizados\Ikea\ikea-app\dist\server.js:168:20) at __webpack_require__ (D:\Trabajo\Programas_Realizados\Ikea\ikea-app\dist\server.js:20:30) at Object. (D:\Trabajo\Programas_Realizados\Ikea\ikea-app\dist\server.js:103:22) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! ikea-app@0.1.0 serve:ssr: node dist/server npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the ikea-app@0.1.0 serve:ssr 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\campe\AppData\Roaming\npm-cache_logs\2019-08-29T23_40_32_333Z-debug.log ` Can you help me to solve it?

Thanks in advance!

kamilmysliwiec commented 5 years ago

Unfortunately, some Node.js packages (especially native bindings) cannot be easily bundled using Webpack. In order to get rid of errors, you have to adjust your webpack configuration (webpack.server.config.js):

const webpack = require('webpack');
const WebpackConfigFactory = require('@nestjs/ng-universal')
  .WebpackConfigFactory;
const nodeExternals = require('webpack-node-externals');

/**
 * In fact, passing following configuration to the WebpackConfigFactory is not required
 * default options object returned from this method has equivalent entries defined by default.
 *
 * Example: WebpackConfigFactory.create(webpack);
 */
const webpackOptions = WebpackConfigFactory.create(webpack, {
  // This is our Nest server for Dynamic universal
  server: './server/main.ts',
});

const whitelistedPackages = /^(?!(livereload|concurrently|mongoose)).*/;
webpackOptions.externals[1] = nodeExternals({
  whitelist: whitelistedPackages,
});

webpackOptions.plugins.push(
  new webpack.IgnorePlugin({
    checkResource(resource) {
      const lazyImports = [
        '@nestjs/microservices',
        'cache-manager',
        'class-validator',
        'class-transformer',
      ];
      if (!lazyImports.includes(resource)) {
        return false;
      }
      try {
        require.resolve(resource);
      } catch (err) {
        return true;
      }
      return false;
    },
  }),
);

module.exports = webpackOptions;

Then, it will work fine (keep in mind that you'll have to install mongoose package alongside your bundle because it's excluded from the compilation).

PS. If you're also using mongodb package directly, you might need to exclude it as well:

const whitelistedPackages = /^(?!(livereload|concurrently|mongoose|mongodb)).*/;