我在尝试将 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/