r/angularjs • • May 22 '15

This AngularJS modules/dependencies thing is a lie

http://michalostruszka.pl/blog/2015/05/21/angular-dependencies-naming-clash/
61 Upvotes

20 comments sorted by

13

u/e82 May 22 '15

I've known this for so long now, that I've sort of forgotten that it can nail new people in the ass.

The last one registered with the name wins - which means you have to be mindful about how you name things, or prefix/namespace them.

The other issue that can happen, is once something is defined as a dependency somehow in your app - every other module can use it, even if it was not an explicit dependency.

angular.module('a',['b','c'];
angular.module('b',[]).factory('bFactory',function(cFactory) { });
angular.module('c',[]).factory('cFactory',function() { });

When you run your app, module b can access cFactory, even though you never defined it as a module dependency.

However, when writing my unit tests - I'll only do a beforeEach(module('b')) - in which case, the unit test would blow up and fail about not knowing about cFactoryProvider. Helps keep me honest about keeping my module dependencies in check and being mindful about where I'm getting things from, even if when running as an app - that sort of goes out the window.

3

u/mrv1234 May 22 '15

It seems there is a global pool of objects for all injections, and that if we load a module with the same name it gets silently overwritten even if it does something completely different. this is apparently a feature and not a bug and is meant for testing. Have a look at this post -> http://victorsavkin.com/post/102965317996/angular-2-bits-unified-dependency-injection Angular 2 will fix this using hierarchical injectors (it looks like it will be transparent for us)

1

u/mostr May 22 '15

Regarding tests, see my comment above (reploy to e82) . This is good for testing on one side indeed, but can be dangerous on the other side.

2

u/e82 May 22 '15

Yeah, I got the impression that the Angular 2 DI seemed to do away with this and can help get towards 'true' modularization.

Went to ng-vegas, and learned quite a few neat things that I've been meaning to do some proof of concept apps with to get my head around it, and also prove to myself that it works the way I think/they say it will.

2

u/mostr May 22 '15

Just out of curiosity, how did you find out about this? Because I can't find it explicitly stated in docs. I had to be kicked in the ass yesterday by the app itself to start digging and to discover that thing.

Regarding tests, the case you describe is good, but there is another one: in tests you use correct dependency because you load "only" modules you need (not all). Tests pass but in the app itself you may end up with completely different dependency being injected because something somewere loaded a module having service with the same name. And this is dangerous I think.

2

u/e82 May 22 '15

Was burned by it once in one of my first larger angular apps. I think I had something like

 angular.module('entityTypeOne',[]).factory('reports', function {});

 angular.module('entityTypeTwo',[]).factory('reports', function {});

and then got tripped up when I wasn't getting the reports that I expected. Thankfully it was the first case/time I ran into the issue - so was a simple change to be 'ok, entityTypeOneReports, entityTypeTwoReports'

You are right though about my testing approach - it does run the risk of - if someone registered two things with the same name, the unit tests would pass but the in-app behavior may be different.

I could do a 'just load the main module' for all of my tests - which I used to do before I know better, but then you run into the issue of all of the initialization code on every module (in the config/run block) firing off - and ended up in an endless rabbit chase of 'mocking a million things that had nothing to do with my test to get the test to pass'.

It's the lesser of two evils - and I've generally worked on 'single-team' projects, so there has been minimal risk of someone introducing a duplicate name for something and it can be easily avoided. But, by avoiding generic names for things can go along way to avoiding this from being an issue.

If you are using the annotation style syntax (which I'm not a fan of, I'd rather let ngAnnotate deal with that for me), I've seen people do things like

angular.module('users',[]).factory('users.reports',function() { });
angular.module('something',['users'].factory('something.service',['users.reports',function(whateverThisDontMatter) { });

to try and 'namespace' their service names - but rather just stick with camelCase.

1

u/BunsOfAluminum May 22 '15

I had this happen when we had developed a module that had a directive in it for rendering list items specific to that module. Then, in another module, we needed another directive to render list items in a format required by that module. The directives just happened to both get named something way too generic, like "ListItem" or something. This was months apart.

We had a hard time figuring out why the list in one module wasn't rendering properly when the directive looked fine. That was when I discovered the naming overlap. I told our contractor to prefix module-specific controllers/services/directives with the module-code from then on.

3

u/[deleted] May 22 '15 edited May 22 '15

DI is more for the reusability of modules overall, not just within your app. You define your dependencies so that if you pick up that module and put it anywhere, not just within the same same app, it's self sustaining.

1

u/SomeRandomBuddy May 22 '15

Jasmine saves my ass every time

1

u/e82 May 22 '15

More of a Mocha man myself ;)

1

u/jeeezu May 23 '15

Im new to AngularJs, but isn't there a naming convention (at least they tell you that at codeschool) to name all service recipes like -

angular.module("<ModuleName>") .factory("<ServiceName>", function <ServiceName>Factory(){ });

so you would not run into issues like that.. Maybe I just missunderstood something but if you name your services like that, it's obvious that there will be a problem?

1

u/joesb May 23 '15

The issue in the article is exactly this problem.

<ServiceName> can clashed with other module, think CacheService or http. Unless you name your service with module prefix like mcCacheService. Which destroys the whole point of having a module.

4

u/kshitagarbha May 22 '15

Well it will be gone soon anyway and we can do this:

import {hello as hello1} from 'thirdParty1';
import {hello as hello2} from 'thirdParty2';

and then just use hello1() hello2() without any DI

You can even do this now if you are using angular 1.4, you are transpiling your ES6 code and if each of the modules exports in the common fashion.

4

u/Capaj May 22 '15 edited May 22 '15

if you are using angular 1.4

Yes, you can use ES6 notation to load files instead of having script tags in html, but to register an injectable, you still have to use angular "module" notation. Quite a difference there.

You're right that it might be best to just use filters, controllers and directives because those are the only means how to manipulate the DOM in angular. Services and factories, you really should not use if you have ES6, AMD or CommonJS modules, because it just makes things more difficult.

I'll have to do a writeup about this.

2

u/elprophet May 22 '15

just use filters, controllers and directives ... Services and factories, you really should not use if you have ES6

That's actually a really good point, and will take developers a long way towards building 1.x apps that easily upgrade to 2.0.

2

u/Swingline0 May 22 '15

We just realized this recently as well. As much as the title sounds click baity, I kind of agree.

The whole module DI concept really leads one to feel like there is a scope provided for all objects but the reality seems that it is more of a global type of situation.

1

u/chazzlabs May 22 '15

My team recently ran into this issue with a couple popular libraries: https://github.com/mgcrea/angular-strap/issues/521

To get around it, we had to tweak our build to rename one of the conflicting components at build time.

1

u/RJCP May 23 '15

TL;DR: watch out for namespace conflicts, be diligent with namespacing your module's components

1

u/jellyman93 Aug 24 '15

So it's just like an "import module" from python or anything else?

I'd agree that it sounds like it should be the way the author said, but this behavior is not unheard of

0

u/YodaLoL May 22 '15

This is one my biggest pet peeves with Angular.