0

I'm trying to make kafkajs external dependency (move out from webpack bundle) with this config:

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

Dependencies:

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

But I'm getting error "Cannot find module":

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

I tried clearing the cache, saving the dependency again and with different externals configuration:

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

Thanks ahead for any guidance

1 Answer 1

2

If you are bundling your node.js app, it is better to mark as external all the node_modules, there is a lib for that, 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
    ...
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.