gpt4 book ai didi

javascript - 使用node中的webpack作为commonjs模块使Kafka.js外部依赖

转载 作者:行者123 更新时间:2023-12-02 21:16:39 24 4
gpt4 key购买 nike

我正在尝试使用此配置使 kafkajs 外部依赖(从 webpack 包中移出):

externals: {
kafkajs: 'kafkajs',
redis: 'redis'
}

依赖关系:

  dependencies: {
...
"kafkajs": "^1.12.0",
...
}

但是我收到错误“找不到模块”:

{
"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>"
}

我尝试清除缓存,再次保存依赖项并使用不同的外部配置:

externals: [
"kafkajs",
{
Kafka: {
commonjs: ["kafkajs", "Kafka"],
}
]

感谢您的指导

最佳答案

如果您要捆绑您的node.js应用程序,最好将所有node_modules标记为外部,有一个库,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
...
};

关于javascript - 使用node中的webpack作为commonjs模块使Kafka.js外部依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60948812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com