📜  npm install webpack - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:44:46.527000             🧑  作者: Mango

npm install webpack - Shell/Bash

Introduction

webpack is a popular module bundler for JavaScript applications. It can transform, bundle, and package code and supporting assets, such as stylesheets and images. In this article, we will discuss how to install webpack using the npm package manager.

Prerequisites

Before installing webpack, you will need to have Node.js installed on your machine. You can download and install Node.js from the official website: https://nodejs.org/.

Installation

To install webpack using npm, open your terminal or command prompt and execute the following command:

npm install webpack --save-dev

This will install webpack in the node_modules directory of your project and add it as a dev dependency to your package.json file.

Usage

After installing webpack, you can use it by running the webpack command in your terminal. By default, webpack will look for a configuration file named webpack.config.js in the root of your project. If you have a different configuration file, you can specify it using the --config option.

Here is an example webpack.config.js file that configures webpack to bundle a single entry point:

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: __dirname + '/dist'
  }
};

To bundle your code with webpack, run the following command:

webpack --mode production

This will bundle your code and create a bundle.js file in the dist directory of your project.

Conclusion

In this article, we have discussed how to install webpack using npm and how to use it to bundle your JavaScript code. With its powerful features and plugins, webpack can help you optimize and organize your code for production.