How to fix 'AngularJs minify Error: [$injector:unpr]'
By FoxLearn 2/11/2025 7:59:05 AM 298
The minification process can cause this error because the names of the dependencies are often shortened, leading to a mismatch in how AngularJS resolves dependencies.
Minify javascript files help you reduce download file size and load your site faster.
If you got an error 'Uncaught Error: [$injector:unpr]' with angularjs after deployment, you can fix the following.
(function () { 'use strict'; var app = angular.module('app', [ // Angular modules 'ngRoute', 'ui.bootstrap' ]); app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) { }]); })();
instead of writing
(function () { 'use strict'; var app = angular.module('app', [ // Angular modules 'ngRoute', 'ui.bootstrap' ]); app.config(function ($routeProvider, $locationProvider) { }); })();
Try to add '$routeProvider', '$locationProvider' to your function as a name, similar $scope, $http...etc in controller, service, factory, directive or you can use $inject as shown below.
(function () { 'use strict'; angular .module('app') .service('accountService', accountService); accountService.$inject = ['$http']; function accountService($http) { } })();
When minifying your code, it will minify all code by replacing the variable name
controller : function($scope) {}
minified
controller : function(e) {}
You should use
controller : ["$scope", function($scope) { ... }]
or using $inject
accountController.$inject = ['$scope', '$uibModal', 'toaster', 'companyService', 'accountService'];
This prevents minification from renaming $scope
or MyService
to shorter names, which would cause AngularJS to fail to resolve them.