How To Install Webpack

 admin
Webpack

Simple tutorial on how to setup webpack v2.

The latest webpack release is: To install the latest release or a specific version, run one of the following commands: npm install--save-dev webpack npm install--save-dev webpack@ If you're using webpack v4 or later, you'll need to install the CLI. Npm install--save-dev webpack-cli Installing locally is what we recommend for most projects. Initialize npm to create a package.json file, then install webpack locally. $ mkdir bootstrap-webpack && cd bootstrap-webpack $ npm init -y $ npm install webpack --save-dev Create the following directory structure for your project, separating the “source” code /src from our bundled “distribution” code /dist.

  1. NPM Install Webpack Plugin Speed up development by automatically installing & saving dependencies with Webpack. It sucks to Ctrl-C your build script & server just to install a dependency you didn't know you needed until now.
  2. If you use webpack for a serious task, and you'd like us to invest more time on it, please donate. This project increases your income/productivity too. It makes development and applications faster and it reduces the required bandwidth.
  3. Initialize npm to create a package.json file, then install webpack locally. $ mkdir bootstrap-webpack && cd bootstrap-webpack $ npm init -y $ npm install webpack --save-dev Create the following directory structure for your project, separating the “source” code /src from our bundled “distribution” code /dist.

Read my blog post on how to setup webpack 2

Table of content

Setup & Installation

Step 1 - Create folder

First let's create a directory called webpack-2-demo and initialize npm:

Step 2 - Install webpack

or do it via Yarn

Step 3 - Write webpack config

Create a webpack.config.js in root of our directory and let's write some configuration.

Now lets add lodash to dependencies in package.json by.

And let's write some code in src/app.js

Step 4 - Run the webpack

To run webpack in development mode.

Screenshot of development server

Total Size: 208KB

or run webpack in production mode.

  • p is for production which uglifies and minifies files.

Screenshot of development server

Total Size: 38KB

Step 5 - Setup webpack development server

Webpack has its own development server. Lets setup that in webpack.config.js by adding the following.

And add the script for bundle.js in src/index.html.

Step 6 - Run development server

Run development server.

Open http://localhost:8080/ in your browser.

Ultimate zip cracker license key. Thats all basic webpack config is done. But what about SASS, IMAGES, ES6 loaders ? How to setup that ? Lets see.

Loaders

Lets set up ES6 + Babel using a webpack loader.

Step 1 - Install babel loader,core & ES6 preset.

After installation, We have to add config to webpack.config.js file.

Step 2 - ES6 Loader

In order to check babel loader, we will change app.js to ES6 syntax.

Run the development server and check the console.

Step 3 - SASS & CSS Loader

Install SASS & CSS Loader

SASS & CSS loader config for webpack is below.

After loaders, final steps are setting up sourcemaps and env for webpack.

Step 7 - Setup Dev & Prod Environment

In package.json file, lets add scripts to run our dev server and build with env.

NODE_ENV=production is environment set for build. Using process.env.NODE_ENV, we can check the env in webpack.

Step 8 - Sourcemap for Dev & Prod

Now we know when we are running production build or development. Lets use it to setup the sourcemap accordingly.

More information on sourcemaps

Final

Final step contains all the config for webpack from above.

Thats all. Thanks for reading my repo.

Articles

Active3 years, 9 months ago

I have installed webpack both globally (first) and for a certain project also (PC, windows 10). Seems that the project dir contains all the folders that it needed:

But when I run the command in the project dir:

It reveals an error:

How to install webpack cli globally

Then I check installed package for node:

It says that's OK (?):

Nevertheless, the error remains.Pls, tell me, can it be fixed and how?!

Just in case, here is the content of the node_modules/webpack/package.json file:

srgg6701srgg6701
6533 gold badges11 silver badges32 bronze badges

1 Answer

When you install a module globally npm install -g webpack (as is required by webpack), it actually becomes available on your command line. So you just need to run:

As for the error which you were getting when running:

that's because when you run node webpack you're actually (supposedly) passing the file webpack.js (or webpack/index.js) to node, hence the 'Cannot find module' error.

How To Install Webpack

laggingreflexlaggingreflex
15.8k21 gold badges99 silver badges160 bronze badges
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.

How To Install Webpack Ise

Not the answer you're looking for? Browse other questions tagged node.jswindowsnpmwebpack or ask your own question.