node.js – How to combine multiple node js file into a single bundle using webpack

I am trying to create a single bundle from multiple javascript file in a nodejs application.

The configuration I am using looks somewhat like this:

const path = require('path')
const nodeExternals = require('webpack-node-externals')

'use strict';

module.exports = {
    externals: [nodeExternals({})],
    entry: './lib/index.js',
    output: {
        iife: false,
        path: path.resolve(__dirname, 'lib'),
        filename: 'bundle.js', // <-- Important
    },
    target: 'node', // <-- Important
};

The problem is when I run bundle.js command instead for it to do what the command says, i get the full source of the file streamed into the terminal.

It seems the file contains some sort of IIFE that gets executed immediately. I set iife: false to false in the webpack configuration but that also did not make any difference.

Any ideas what could be wrong?

Edit:

I am calling webpack by adding:
bundle: webpack --config webpack.config.js to script section in package.json and then I run npm run bundle

Read more here: Source link