GithubHelp home page GithubHelp logo

Comments (5)

kresdjan avatar kresdjan commented on April 26, 2024 1

Plus one.

from ng-universal.

robstadnick avatar robstadnick commented on April 26, 2024

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();

from ng-universal.

robstadnick avatar robstadnick commented on April 26, 2024

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

from ng-universal.

ccampetella-clovinn avatar ccampetella-clovinn commented on April 26, 2024

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

[email protected] build:ssr D:\Trabajo\Programas_Realizados\Ikea\ikea-app
npm run build:client-and-server-bundles && npm run compile:server

[email protected] 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]

[email protected] 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]
+ 1992 hidden modules

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

[email protected] 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! [email protected] serve:ssr: node dist/server
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 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!

from ng-universal.

kamilmysliwiec avatar kamilmysliwiec commented on April 26, 2024

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)).*/;

from ng-universal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.