使用MongoDB和Angular universal / webpack的关键依赖关系

时间:2018-01-14 07:29:17

标签: node.js mongodb webpack angular-universal

我在尝试将mongodb软件包放入我的角度通用项目时遇到了一些错误。

我想知道是否有人为此解决了问题,或者知道有些人因为我无法找到它而被发布。

从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'),
      {}
    )
  ]
}

2 个答案:

答案 0 :(得分:1)

我昨天尝试使用猫鼬进行角度通用启动时遇到了同样的问题。首先,我尝试使用像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'),
      {}
    )

我在警告中为每个库添加了一个插件(刚刚在正则表达式中使用'mongoose','mongodb','mongodb-core'和'require_optional'更改了'express')。这删除了警告,但服务器在启动时出现了一些关于某个缺失模块的奇怪错误(该模块的名称类似于'。')。很可能是我误用了这个插件,我不确定,但我没有心情再挖掘它了,我只是尝试了webpack-node-externals库解决了警告并让服务器再次顺利启动。

希望有所帮助!干杯

答案 1 :(得分:0)

停止使用 angular.json 中的选项 {{1}} 捆绑服务器依赖项