带有nestjs的Angular-universal:错误:找不到模块'./drivers/node-mongodb-native/connection'

Posted

技术标签:

【中文标题】带有nestjs的Angular-universal:错误:找不到模块\'./drivers/node-mongodb-native/connection\'【英文标题】:Angular-universal with nestjs: Error: Cannot find module './drivers/node-mongodb-native/connection'带有nestjs的Angular-universal:错误:找不到模块'./drivers/node-mongodb-native/connection' 【发布时间】:2020-04-08 08:58:57 【问题描述】:

我将 Angular-universal 与 Nest-js 一起使用,它在本地运行良好,但是当我将它部署到 Heroku 上时,我收到了这个错误:

Error: Cannot find module './drivers/node-mongodb-native/connection'.

我不知道是什么原因造成的。 这是我的 package.json:


  "name": "personal-finance",
  "version": "0.0.0",
  "scripts": 
    "ng": "ng",
    "start": "npm run serve:s-s-r",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "serve": "node serve-script",
    "compile:server": "webpack --config webpack.server.config.js --progress --colors",
    "serve:s-s-r": "node dist/server",
    "build:s-s-r": "npm run build:client-and-server-bundles && npm run compile:server",
    "build:client-and-server-bundles": "ng build --prod && ng run personal-finance:server:production",
    "heroku-postbuild": "npm run build:s-s-r"
  ,
  "private": true,
  "dependencies": 
    "@angular/animations": "~8.2.14",
    "@angular/cdk": "~8.2.3",
    "@angular/common": "~8.2.14",
    "@angular/compiler": "~8.2.14",
    "@angular/core": "~8.2.14",
    "@angular/forms": "~8.2.14",
    "@angular/material": "^8.2.3",
    "@angular/platform-browser": "~8.2.14",
    "@angular/platform-browser-dynamic": "~8.2.14",
    "@angular/platform-server": "~8.2.14",
    "@angular/router": "~8.2.14",
    "@fortawesome/angular-fontawesome": "^0.5.0",
    "@fortawesome/fontawesome-svg-core": "^1.2.26",
    "@fortawesome/free-solid-svg-icons": "^5.12.0",
    "@nestjs/common": "^6.10.11",
    "@nestjs/core": "^6.10.11",
    "@nestjs/jwt": "^6.1.1",
    "@nestjs/mongoose": "^6.1.2",
    "@nestjs/ng-universal": "^2.0.1",
    "@nestjs/passport": "^6.1.1",
    "@nestjs/platform-express": "^6.10.11",
    "@nguniversal/express-engine": "^8.0.0",
    "@nguniversal/module-map-ngfactory-loader": "^8.0.0",
    "@types/node": "~8.9.4",
    "bcrypt": "^3.0.7",
    "cleave.js": "^1.5.3",
    "dotenv": "^8.2.0",
    "hammerjs": "^2.0.8",
    "mongodb": "^3.4.0",
    "mongoose": "^5.8.1",
    "ngx-cleave-directive": "^1.1.3",
    "passport": "^0.4.0",
    "passport-jwt": "^4.0.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "6.5.3",
    "ts-node": "~7.0.0",
    "tslib": "^1.10.0",
    "typescript": "~3.5.3",
    "zone.js": "~0.9.1"
  ,
  "devDependencies": 
    "@angular-devkit/build-angular": "~0.803.20",
    "@angular/cli": "~8.3.20",
    "@angular/compiler-cli": "~8.2.14",
    "@angular/language-service": "~8.2.14",
    "@types/dotenv": "^8.2.0",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "^5.0.0",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "nodemon": "^1.18.11",
    "protractor": "~5.4.0",
    "rimraf": "^2.6.3",
    "ts-loader": "^5.2.0",
    "tslint": "~5.15.0",
    "wait-on": "^3.2.0",
    "webpack-cli": "^3.1.0"
  ,
  "engines": 
    "node": "12.13.1",
    "npm": "6.12.1"
  

【问题讨论】:

【参考方案1】:

我临时固定添加,在路径\node_modules@nestjs\ng-universal\dist\webpack-config.factory.js,mongoose|mongodb到

 whitelist: /^(?!(@nestjs\/(common|core|microservices)|livereload|concurrently)).*/

这样

 whitelist: /^(?!(@nestjs\/(common|core|microservices)|livereload|concurrently|mongoose|mongodb)).*/

或者用这种方式更新webpack.config.js

const webpack = require('webpack');
const WebpackConfigFactory = require('@nestjs/ng-universal')
  .WebpackConfigFactory;
const nodeExternals = require('webpack-node-externals');
const webpackOptions = WebpackConfigFactory.create(webpack, 
  server: './server/main.ts',
  prerender: './prerender.ts', 
);

const whitelistedPackages =  /^(?!(@nestjs\/(common|core|microservices)|livereload|concurrently|mongoose|mongodb)).*/;
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;

这篇文章可以帮助https://github.com/nestjs/ng-universal/issues/115#issuecomment-527403032

【讨论】:

以上是关于带有nestjs的Angular-universal:错误:找不到模块'./drivers/node-mongodb-native/connection'的主要内容,如果未能解决你的问题,请参考以下文章

带有模拟存储库的 Nestjs 服务测试不起作用

如何在带有 HTML 渲染的 NestJs 中使用验证?

使用带有 NestJs 微服务的套接字客户端

使用带有 TypeOrm 的 NestJS 连接 MySQL 数据库时出现问题

如何在带有typeorm的nestjs项目中使用外部实体?

带有 Knex 的 nestjs:无法在生产中迁移和运行种子