node.js - 使用 MongoDB 和 Angular universal/webpack 的关键依赖

标签 node.js mongodb webpack angular-universal

我在尝试将 mongodb 包引入我的 angular 通用项目时遇到了一些错误。

我想知道是否有人对此有解决方案,或者知道有人发布了它,因为我找不到任何解决方案。

代码取自 webpack 文档:

var cache = {};
function importAll (r) {
  console.log("importing " +r);
  r.keys().forEach(key => cache[key] = r(key));
}

importAll(require.context('mongodb', true, /\.js$/));

错误:

WARNING in ./node_modules/mongodb/lib/collection.js
2997:23-30 Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
 @ ./node_modules/mongodb/lib/collection.js
 @ ./node_modules/mongodb \.js$
 @ ./server.ts

WARNING in ./node_modules/require_optional/index.js
82:18-42 Critical dependency: the request of a dependency is an expression
 @ ./node_modules/require_optional/index.js
 @ ./node_modules/mongodb-core/index.js
 @ ./node_modules/mongodb/index.js
 @ ./node_modules/mongodb \.js$
 @ ./server.ts

WARNING in ./node_modules/require_optional/index.js
90:20-44 Critical dependency: the request of a dependency is an expression
 @ ./node_modules/require_optional/index.js
 @ ./node_modules/mongodb-core/index.js
 @ ./node_modules/mongodb/index.js
 @ ./node_modules/mongodb \.js$
 @ ./server.ts

WARNING in ./node_modules/require_optional/index.js
97:35-67 Critical dependency: the request of a dependency is an expression
 @ ./node_modules/require_optional/index.js
 @ ./node_modules/mongodb-core/index.js
 @ ./node_modules/mongodb/index.js
 @ ./node_modules/mongodb \.js$
 @ ./server.ts

Webpack cfg 文件///................................................ …………///

const path = require('path');
const webpack = require('webpack');

module.exports = {
  entry: {
    // This is our Express server for Dynamic universal
    server: './server.ts',
    // This is an example of Static prerendering (generative)
    prerender: './prerender.ts'
  },
  target: 'node',
  resolve: {
    extensions: ['.ts', '.js'],
    alias: {
      'pg-native': path.join(__dirname, 'aliases/pg-native.js'),
      'pgpass$': path.join(__dirname, 'aliases/pgpass.js'),
    }
  },
  // Make sure we include all node_modules etc
  externals: [/(node_modules|main\..*\.js)/m],
  output: {
    // Puts the output at the root of the dist folder
    path: path.join(__dirname, 'dist'),
    filename: '[name].js'
  },
  module: {
    rules: [
      { test: /\.ts$/,
        loader: 'ts-loader'}
    ]
  },
  plugins: [
    new webpack.ContextReplacementPlugin(
      // fixes WARNING Critical dependency: the request of a dependency is an expression
      /(.+)?angular(\\|\/)core(.+)?/,
      path.join(__dirname, 'src'), // location of your src
      {} // a map of your routes
    ),
    new webpack.ContextReplacementPlugin(
      // fixes WARNING Critical dependency: the request of a dependency is an expression
      /(.+)?express(\\|\/)(.+)?/,
      path.join(__dirname, 'src'),
      {}
    )
  ]
}

最佳答案

昨天我在尝试使用 mongoose 的角度通用启动器时遇到了同样的问题。首先,我尝试在 webpack.server.config.js 中使用 ContextReplacementPlugin webpack 插件。

new webpack.ContextReplacementPlugin(
      // fixes WARNING Critical dependency: the request of a dependency is an expression
      /(.+)?express(\\|\/)(.+)?/,
      path.join(__dirname, 'src'),
      {}
    )

我在警告中为每个库添加了一个插件(只是将正则表达式中的“express”更改为“mongoose”、“mongodb”、“mongodb-core”和“require_optional”)。这删除了警告,但服务器在启动后立即失败,并出现有关某些丢失模块的奇怪错误(该模块被命名为“.”)。很可能我滥用了插件,我不确定,但我没有心情再深入研究它,我只是尝试了 webpack-node-externals解决了警告并让服务器再次顺利启动的库。

希望对您有所帮助!干杯

关于node.js - 使用 MongoDB 和 Angular universal/webpack 的关键依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48247518/

相关文章:

javascript - 如何制作超过 5 个输入的松弛对话? Node 松弛API

mysql - 如何使用 MEAN 堆栈(MySQL、Express、Angular、Node)将数据插入数据库

javascript - 为什么数组的旧内容被删除了?

javascript - 为什么我无法从 Node.js 中的扩展 EventEmitter 类捕获事件?

mongodb - 如何根据 Mongo 中的现有值更新对象数组中的属性?

node.js - 无法使用 @ngtools/webpack 创建 Angular 5 通用服务器应用程序应用程序包

mongodb - com.mongodb.MongoException:无法与主服务器和重试通话

javascript - 异常的标识符与等待 : Node. js

javascript - Webpack 2/Babel 错误的源映射(ES5 而不是 ES6)

Webpack-dev-server 提供旧文件内容