question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using this with the Mongoose module

See original GitHub issue

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:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
kresdjancommented, Aug 18, 2019

Plus one.

0reactions
kamilmysliwieccommented, Sep 3, 2019

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)).*/;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Express Tutorial Part 3: Using a Database (with Mongoose)
This article briefly introduces databases, and how to use them with Node/Express apps. It then goes on to show how we can use...
Read more >
What are the advantages of using Mongoose module?
Mongoose module is one of the most powerful external modules of NodeJS. Mongoose is a MongoDB ODM i.e (Object database Modelling) that used ......
Read more >
Mongoose - npm
Start using mongoose in your project by running `npm i mongoose`. There are 13129 other projects in the npm registry using mongoose.
Read more >
Mongoose v6.8.1: Getting Started
First be sure you have MongoDB and Node.js installed. Next install Mongoose from the command line using npm : $ npm install mongoose...
Read more >
Getting Started with MongoDB & Mongoose
Mongoose is an ODM (Object Data Modeling) library for MongoDB. While you don't need to use an Object Data Modeling (ODM) or Object ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found