How to add dependency in webpack?

by herminia_bruen , in category: Javascript , 7 months ago

How to add dependency in webpack?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by brandy , 7 months ago

@herminia_bruen 

To add a dependency in webpack, follow these steps:

  1. Install the package you want to add as a dependency by running the following command in your terminal:
1
npm install package-name --save


Replace package-name with the actual name of the package.

  1. After installing the package, webpack will automatically include it as a dependency in your project's package.json file.
  2. You can then import and use the package in your JavaScript files by adding the following line at the top of the file:
1
import packageName from 'package-name';


Replace packageName with the variable name you want to assign to the imported package.

  1. Finally, you can build your project using webpack to bundle all dependencies together into a single output file. Run the following command in your terminal:
1
webpack


This will generate a dist folder with the bundled output file that includes all dependencies.