MMTTMM
2022-06-09 09:55:47
我正在嘗試使用此配置制作 kafkajs 外部依賴項(xiàng)(從 webpack 包中移出):externals: { kafkajs: 'kafkajs', redis: 'redis'}依賴項(xiàng): dependencies: { ... "kafkajs": "^1.12.0", ... }但我收到錯(cuò)誤“找不到模塊”:{ "error": "Initialization has failed due to: Error: Cannot find module 'kafkajs'\n at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)\n at Function.Module._load (internal/modules/cjs/loader.js:507:25)\n at Module.require (internal/modules/cjs/loader.js:637:17)\n at require (internal/modules/cjs/helpers.js:22:18)\n at Object.<anonymous>"}我嘗試清除緩存,再次保存依賴項(xiàng)并使用不同的外部配置:externals: [ "kafkajs", { Kafka: { commonjs: ["kafkajs", "Kafka"], }]提前感謝您的任何指導(dǎo)
1 回答

紅顏莎娜
TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個(gè)贊
如果您正在捆綁您的 node.js 應(yīng)用程序,最好將所有 node_modules 標(biāo)記為外部,有一個(gè) lib 用于此,webpack-node-externals。
npm install webpack-node-externals --save-dev
//webpack.config.js
const nodeExternals = require('webpack-node-externals');
...
module.exports = {
...
target: 'node', // in order to ignore built-in modules like path, fs, etc.
externals: [nodeExternals()], // in order to ignore all modules in node_modules folder
...
};
添加回答
舉報(bào)
0/150
提交
取消