How to specify an output filename with the Webpack CLI?

Viewed 1760

I want to create a file named background.js inside my build folder. This is the command I use:

webpack --mode production ./src/background -o ./build/background.js

But this creates a folder named background.js inside my build folder. I didn't find any other flag on the webpack-cli documentation. So how do I specify an output filename with the Webpack CLI?

2 Answers

in the webpack.config.js folder add this on the top,

const path = require("path");

then your Module exports will be:

module.exports = {
module: {
    rules: [ 
        // your rules, eg, babel loder.
    ],
},
    // the main solution
entry: "./src/index.js", // your entry file that you want it to be bundled for production
output: { // the output file path,
    path: path.resolve(__dirname, "build"),// build is your folder name.
    filename: "background.js", // your specific filename to be built in the build folder.
}, };

and finally your package.json build script will be

"build": "webpack --mode production",
Related