Grunt build to nginx causes errors (angularJS) - angularjs

I have a simple AngularJS application for which I used grunt serve to scaffold the code and test it. I now want the code deployed to a server using nginx and I'm doing it with the grunt build task which generates the code in the ./dist folder. I now want this code transferred to the server where it can be hosted.
I don't know if the error is related to minifying the code, but the app doesn't run.
The errors are:
Error: [$injector:unpr] Unknown provider: aProvider <- a
http://errors.angularjs.org/1.2.6/$injector/unpr?p0=aProvider%20%3C-%20a
at http://localhost/kds/scripts/ded94bd9.vendor.js:3:30474
at http://localhost/kds/scripts/ded94bd9.vendor.js:4:13890
at Object.c [as get] (http://localhost/kds/scripts/ded94bd9.vendor.js:4:13194)
at http://localhost/kds/scripts/ded94bd9.vendor.js:4:13985
at c (http://localhost/kds/scripts/ded94bd9.vendor.js:4:13194)
at d (http://localhost/kds/scripts/ded94bd9.vendor.js:4:13440)
at Object.e [as instantiate] (http://localhost/kds/scripts/ded94bd9.vendor.js:4:13587)
at http://localhost/kds/scripts/ded94bd9.vendor.js:4:29734
at http://localhost/kds/scripts/ded94bd9.vendor.js:4:22719
at f (http://localhost/kds/scripts/ded94bd9.vendor.js:3:30909)
What's going wrong here?
EDIT Also, on the Chrome network log: http://cl.ly/image/3z0v3X2n1f3h
And the conf section of nginx.conf:
location /kds/ {
alias /Users/asheshambasta/code/kds/dist/;
index index.html index.htm;
}
And grunt serve loads up the application without problems.

When you see an error in AngularJS like "Unknown provider: aProvider <- a" or "Unknown provider: nProvider <- n", it means the AngularJS dependency injection system was not able to match an argument to a provider. This error is legitimate when you have an argument in an injected function that does not exist, but...
It more often than that, means your AngularJS code was minified, which requires some work. You will have to annotate your controllers/services/etc in a certain way for AngularJS's dependency injection system to find it. See the docs (search for "minification") for more details, but here's a quick rundown:
// This injects $scope and $http as the two arguments to the controller.
myApp.controller("MyCtrl", ["$scope", "$http", function($scope, $http) { ... }]);
The other option is to disable minification in your build configuration.

Related

Error while loading controllers - migrating AngularJS1 to Angular6

I'm trying to do upgrade components written in AngularJS1 to Angular6. I'm taking the approach of having the wrappers for all the existing AngularJS1 component by extending "UpgradeComponent" placed under the folder "directive-wrappers" in my example. When I try to add some controllers which doesn't have any directives, I get the error message
Error: [$injector:unpr] Unknown provider: testDirective2DirectiveProvider <- testDirective2Directive
https://errors.angularjs.org/1.7.8/$injector/unpr?p0=testDirective2DirectiveProvider%20%3C-%20testDirective2Directive
at eval (angular.js:138)
at eval (angular.js:4924)
at Object.getService [as get] (angular.js:5084)
at eval (angular.js:4929)
at Object.getService [as get] (angular.js:5084)
at Function.UpgradeHelper.getDirective (upgrade_helper.ts:56)
at new UpgradeHelper (upgrade_helper.ts:52)
at TestDirective2Wrapper.UpgradeComponent (upgrade_component.ts:106)
at new TestDirective2Wrapper (TestDirective2Wrapper.ts:27)
at createClass (provider.ts:265) "<app-root _nghost-c69="">"
I tried adding studentController and homePageController, but not able to load it. Any ideas why I'm facing this issue?
https://stackblitz.com/edit/ng6hybrid-c8h6uv
There are two issues that need to be addressed. First, in the studentController.js file, you are using the var mainApp = angular.module("testApp", []); syntax which is resetting the AngularJS module and everything that got instantiated before is being removed.
To answer your question about the controllers, AngularJS controllers can't be upgraded directly using UpgradeComponent (see https://angular.io/guide/upgrade#using-angularjs-component-directives-from-angular-code) because they aren't components/directives that have a template. That is why the error you get (after fixing the first issue) is Unknown provider: studentControllerDirectiveProvider <- studentControllerDirective. It can't find a directive for the reference studentController, so it is adding Directive to see if that has anything.
To fix this, either convert the controller in AngularJS to a directive with a template, or convert it to a service in AngularJS and then inject it into Angular using the UpgradeModule. Keep in mind you will need a template somewhere for this controller so it can be upgraded to Angular. Please see a working example of this in my stackblitz below.
https://stackblitz.com/edit/ng6hybrid-zs6waw?file=app%2Fdirective-wrappers%2FstudentControllerWrapper.ts

Issue with loading Angular $window

Hi I'm trying to use $window object in the module below.
var testInterceptor = function($provide,$httpProvider,$window){
// actual Code
}
angular.module('MyApp')
.config(testInterceptor);
But the page is throwing the error like below
Uncaught Error: [$injector:modulerr] Failed to instantiate module MyApp due to:
Error: [$injector:unpr] Unknown provider: $window
Please help me to resolve this issue.
$window is a service and can't be used in config phase.
Only providers and constants can be used here.
Read the documentation. https://docs.angularjs.org/guide/module
Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.
What you could do is place your necessary code in the run phase. Because you can use services here.
Try this one. (http://codepen.io/anon/pen/jWRmqZ)
var testInterceptor = function($provide,$httpProvider,$window){
// More Code
}
angular.module('MyApp', [])
.config(['$provide','$httpProvider','$window', testInterceptor]);

Angularjs debugging of [$injector:unpr] Unknown provider: e?

How can I debug Angular's error:
Uncaught Error: [$injector:modulerr] Failed to instantiate module zvApp due to:
Error: [$injector:unpr] Unknown provider: e
http://errors.angularjs.org/1.4.3/$injector/unpr?p0=e
at http://app.dev/build/js/vendor-53eec796.js:3:19384
There is some provider unknown, but which one? I can't find an easy way how to debug this. Tried it all.. Think it's a bug in a 3rd party package, but can't be sure without debugging.
Most possibly your issue is with lack of explicit dependency annotation (unless ng-annotate is used with minification), using array syntax (.service('myService', ['$http', function($http){...) or $inject static property (MyService.$inject=['$q']) on the constructor. It is very hard to find it late in the game or in the minified code. So general practice would be to use ng-strict-di with ng-app directive and while your development or running application with non minified code it will throw more informative error in the places where you lack explicit dependency annotation.
Example:-
If using ng-app then,
<html ng-app="myApp" ng-strict-di>
If manually bootstrapping, then specify it as option.
angular.bootstrap(document, ['myApp'], {
strictDi: true
});
By turning on strict-di angular injector will specifically check for explicit annotation before instantiating and lack of which will break the application with error, this more often helps prevent these issue or catch them early in the game. Generally turning on strict-di option is helpful (and no need to remove it for production btw) in catching lack of dependency injection almost every definition including run, config and even the resolve functions (used with routers, modals etc).

Error: [$injector:unpr] Unknown provider: $$rAFProvider

Using Karma to test Angular getting error:
Error: [$injector:modulerr] Failed to instantiate module ngMock due to:
Error: [$injector:unpr] Unknown provider: $$rAFProvider
Angular mock, Angular versions error?
I've heard solutions detailing changing of angular-mock version or angular version--which seems like a bad idea since I'd like to test on the same version as the app starts with.
Anyone else have an error like this?
Please check your angular version.
Your angular version and angular-mock version must be same.
To download angular-mock go the below link and replace the X.Y.Z with your angular version and save it
http://ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-mocks.js
for example my angular version is 1.2.9
This work for me .
This issue was resolved by making sure the rails asset pipeline, which provides the angular-related assets in my case, was correctly serving assets during testing.

Bug elixir gulp --production angularjs

I working with Laravel 5 and Angularjs, Laravel 5 uses Gulp for schedule asset's task and all work fine when use gulp's command, but when I use gulp --production's command and gulp minimize the Angularjs library, the console's chrome output:
Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:unpr] Unknown provider: e
Below my code:
gulpfile.js
First step, I copy all javascripts from bower folder to assets/js
.copy(
'resources/assets/bower_resources/angular/angular.js',
'resources/assets/js/vendor/angular.js')
.copy(
'resources/assets/bower_resources/angular-bootstrap/ui-bootstrap.js',
'resources/assets/js/vendor/ui-bootstrap.js')
.copy(
'resources/assets/bower_resources/ng-busy/src/angular-busy.js',
'resources/assets/js/vendor/angular-busy.js')
.copy(
'resources/assets/bower_resources/angular-ui-notification/dist/angular-ui-notification.js',
'resources/assets/js/vendor/angular-ui-notification.js')
second step, I put together all libraries copied inside one file "libraries.js"
.scripts(['vendor/angular.js',
'vendor/ui-bootstrap.js',
'vendor/angular-busy.js',
'vendor/angular-ui-notification.js',
'app.js'],'public/scripts/libraries.js')
My app.js
var app = angular.module('app', ['ui.bootstrap','ngBusy','ui-notification']);
app.config(function ($interpolateProvider) {
$interpolateProvider.startSymbol('[[');
$interpolateProvider.endSymbol(']]');
});
I try different combination, but not working.
Please help!
Have you tried using inline annotation for dependency injection?
Here's a previous question that solves this problem
"Uncaught Error: [$injector:unpr]" with angular after deployment

Resources