1. 5
    AngularJS with Webpack - Introduction
    4m 20s
⚠️ This lesson is retired and might contain outdated information.

AngularJS with Webpack - Introduction

Kent C. Dodds
InstructorKent C. Dodds
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 2 years ago

Webpack is a module bundler for the web. It is incredibly powerful and enables modularity in angular applications. This is the first of several lessons to get you up and going with webpack in Angular applications.

[00:00] The first thing that we're going to want to do, obviously, is install Webpack. We'll also install webpack-dev-server. We'll install these as dev dependencies, so we'll add that -D. Then we'll create a webpack.config.js with a module.exports, our config object.

[00:22] We'll set our context to be the context of our application. This will be...Whoops. Need to spell that correctly. dirname plus /app. The context of our app is right there in the app directory. Our entry file is going to be index.js. That will be in relation to our context.

[00:45] We'll put our index.js there, and then we'll have our output, where the path is the same -- dirname plus /app. The filename will be bundle.js. That's it for our webpack.config. We're going to create our index.js here with an alert of "Hello."

[01:11] We'll run Webpack. This will create the bundle for us. In our index.html, we'll add that bundle as a script, where the src is bundle. We save. If we refresh, we get the "Hello" there. We're going to actually delete this bundle, the file, just to illustrate something here.

[01:38] Instead of using Webpack, we want to have a server that will automatically rebuild every single time. We can accomplish this same thing with Webpack watch, but we're going to use webpack-dev-server instead.

[01:52] I recommend in your package.json, you'll add a script called "start," where it points to the webpack-dev-server in your local known modules, with a content base pointing to your context or where your app will be served up.

[02:10] With that configuration, if I say "npm start," it will run that script for me. Now it's serving up my app on port 8080. If I switch this to localhost:8080, I get the "Hello." Now we're running using webpack-dev-server.

[02:28] Again, you'll notice that the bundle.js is not inside the app directory, but it's still running. That is because webpack-dev-server is actually serving that file in memory. When a request comes for that bundle.js, it will send that file to us instead.

[02:47] Now let's make this an Angular app. The first thing we'll do is we'll say, "ng-app="app." Inside of our index.js, we are going to require dependencies a little bit different than you may be used to. The first thing we'll do is we'll npm install angular, and in here, we'll say, "var angular = require('angular')." That's common JS syntax that Webpack enables us to do.

[03:16] We'll create our app module with no dependencies. From here, everything should work if I assign this to ngModule and then say "console.log(ngModule)." Save, refresh. In here, we have our object, which is our module. We also have the Angular logo up there. This is an Angular app.

[03:44] Everything is up and running with Webpack using this configuration here, where the context is pointing to the app directory where our app is living. We have an entry of our index.js file, and then our output is pointing to the same directory and a bundle.js file. In here, in our index.html, we're referencing that bundle.js file.

[04:15] That is how you get started with Angular and Webpack. More to come.

James
James
~ 9 years ago

I get this error when running npm start

basedir=dirname "$0" ^ SyntaxError: Unexpected token ILLEGAL at exports.runInThisContext (vm.js:73:16) at Module._compile (module.js:443:25) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Function.Module.runMain (module.js:501:10) at startup (node.js:129:16) at node.js:814:3

Kent C. Dodds
Kent C. Doddsinstructor
~ 9 years ago

I'm sorry James, I'll need a little more context to be able to help you. Can you give me any kind of reproduction steps?

Willie  Streeter
Willie Streeter
~ 9 years ago

Hi Kent, I was wondering how you went about setting up the test server. Do you use 'webpack-dev-server'? I do note see it set up in the config and it appears as though your webstorm configuration does not show debug or run configuration.

Thanks will

Kent C. Dodds
Kent C. Doddsinstructor
~ 9 years ago

Here's the npm script for running npm start https://github.com/eggheadio/webpack-angular/blob/finished/intro/package.json#L9

Omer Koren
Omer Koren
~ 9 years ago

I've followed the video:

  1. Created a new directory "gridApp" with /app in it.
  2. Inside "gridApp" I've typed "npm install webpack webpack-dev-server -D
  3. This did not create node_modules for me, so I did the same with --save instead of -D.
  4. Now I continued the tutorial, and created the other config files.
  5. when I typed "webpack" in the cmd, it said "webpack" is not recognized.
  6. Did npm install webpack -g, and now I have cli
  7. I run webpack, but it has problems with windows path I guess. Says: ERROR in Entry module not found: Error: Cannot resolve module 'index.js' in C:\Users\Yonk\Documents\gridApp/app resolve module index.js in C:\Users\Yonk\Documents\gridApp/app Any idea how to get over this one?

P.S. I'm not using webstorm on the current workstation

Omer Koren
Omer Koren
~ 9 years ago

My bad... forgot the './' in the entry file.

Kent C. Dodds
Kent C. Doddsinstructor
~ 9 years ago

Sounds like you're missing your entry file index.js. Does your app directory have an index.js?

Kent C. Dodds
Kent C. Doddsinstructor
~ 9 years ago

Ah, great :-)

Kumar Pratik
Kumar Pratik
~ 9 years ago

It's my humble request that you guys should do a video on Lazy loading of directives, controllers and other components from another module.

Kent C. Dodds
Kent C. Doddsinstructor
~ 9 years ago

Fantastic idea. Until I can get to it, I recommend you take a look at John's lessons on ocLazyLoad

Van
Van
~ 9 years ago

Hi, i get the error

ERROR in ./app/index.js Module not found: Error: Cannot resolve module 'angular' in .../webpack-angular/app @ ./app/index.js 1:14-32

angular is still on the package.json as dependencies.

in the index.js the same as in the video

var angular = require('angular'); var ngModule = angular.module('app', []); console.log(ngModule);

Kent C. Dodds
Kent C. Doddsinstructor
~ 9 years ago

First thing I would double check is that you can find the angular directory in your node_modules. Make sure you not only have angular referenced in your dependencies in your package.json, but that you've also run $ npm install and that the angular directory appears in your node_modules. Once you're sure of that, fire up webpack again and see if that works.

Clinton
Clinton
~ 9 years ago

Hey James,

I am on a windows machine. Not sure if that has anything to do with it. Anyways, try changing the package.json "start" line to: "start": "webpack-dev-server --content-base app",

It worked for me. Here is the reference: https://github.com/webpack/webpack/issues/1020

Van
Van
~ 9 years ago

The problem my me is solved by reinstalling python 2.7, cause i uninstalled it before. Thanks!

milad
milad
~ 9 years ago

The first thing I hear when I start a tutorial normally is a explanation of "What we're going to learn and why ". But in your tutorials , you just start coding ! no explanation , no nothing . Seems you've written your codes somewhere and you just copy and paste it here , with no explanation. What is webpack ? Why we should use it ? What is it good for ?

hima
hima
~ 8 years ago

I agree they start with no explanation... i don't know why most videos in egghead start like that..

Kent C. Dodds
Kent C. Doddsinstructor
~ 8 years ago

This lesson series assumes you already know what Webpack and angular are. If you don't know what Webpack is, you may benefit from this playlist: https://egghead.io/playlists/learning-webpack

Kent C. Dodds
Kent C. Doddsinstructor
~ 8 years ago

Sorry, I don't remember getting notified of your response. See this: https://egghead.io/forums/lesson-discussion/topics/angular-with-webpack-introduction#post-2552

Nate Gibbons
Nate Gibbons
~ 8 years ago

Just an FYI. In your npm scripts - you don't need to give the full path the the node_modules/.bin directory. npm scripts resolve anything in the .bin automatically. so you could just have:

"start": "webpack-dev-server --content-base app"

Andrew
Andrew
~ 8 years ago

I'm have an issue with reloading once I've set up the webpack-dev-server. When the dev-server is running and I add the console.log(ngModule) line to my code save it and refresh the browser the changes never get picked up. After fussing with it for about an hour I decided to download from github your code and switched to the finished/intro branch and tried the same thing, but still couldn't get webpack-dev-server to pick up the change after it was already started. The only way I can get it to pick up the changes is by restart webpack-dev-server. Do you have any ideas as what would be causing this. I'm on a windows 7 pc running webstorm 11.0.1. Also, I did do your tutorial on the basic webpack intro where everything is in node's route (./) and not and any sub folders and it was able to pick up the changes that way. Not sure if that's helpful. Any advice would be appreciated.

Kent C. Dodds
Kent C. Doddsinstructor
~ 8 years ago

Huh, that's odd. I'd recommend you reach out to the webpack support channels to see if they can help you there. Good luck!

Andrew
Andrew
~ 8 years ago

Thanks Kent!

Found the solution. For windows users you have to use absolute path in the config file.

Reference: https://webpack.github.io/docs/troubleshooting.html

Aid19801
Aid19801
~ 8 years ago

I'm probably being stupid here but i cannot get webpack to fire up just by typing 'webpack' into the terminal. Did you miss out a step or am i missing something myself?

Tim
Tim
~ 8 years ago

I had the same problem, I just installed globally and it worked just fine. npm install webpack -g

Ricardo Hofstetter Dias
Ricardo Hofstetter Dias
~ 8 years ago

I was having trouble running npm start, had to remove the "node node_modules/.bin/" portion from the package.json script. Just "start": "webpack-dev-server --content-base app", worked fine. Hope it helps someone (ps: i'm on windows).

Sergei
Sergei
~ 8 years ago

You might want to include the windows code for the npm start and npm build since both provided examples on github only work on unix based systems. At least as commented out code on the repo.

Zeeshan
Zeeshan
~ 7 years ago

Hi, I'm running Webpack with angular-i18n and angular-dynamic-locale. Issue is that when I build the project for distribution, the localization for the time is not loading correctly. Everything is loading into the browser and the dist folder correctly (so no files are missing), but the locale is not changing the time to the correct format. It's working fine in development however.

Any thoughts on best practices for this?

My webpack.config.js is loading the .js files as follows:

test: /angular-locale_.+js$/, loader: 'file-loader?name=[path][name].[ext]?[hash]'

Manuel Carrera
Manuel Carrera
~ 7 years ago

When I run my start script and load my page, I get an error that require is not defined. What am I missing?

Markdown supported.
Become a member to join the discussionEnroll Today