{"record":{"id":"3fd88a228aa23c88","repo":"angular/angular.js","slug":"injector-already-created-can-not-register-a-modul","errorCode":null,"errorMessage":"Injector already created, can not register a module!","messagePattern":"Injector already created, can not register a module!","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/ngMock/angular-mocks.js","lineNumber":3091,"sourceCode":"   * This function registers a module configuration code. It collects the configuration information\n   * which will be used when the injector is created by {@link angular.mock.inject inject}.\n   *\n   * See {@link angular.mock.inject inject} for usage example\n   *\n   * @param {...(string|Function|Object)} fns any number of modules which are represented as string\n   *        aliases or as anonymous module initialization functions. The modules are used to\n   *        configure the injector. The 'ng' and 'ngMock' modules are automatically loaded. If an\n   *        object literal is passed each key-value pair will be registered on the module via\n   *        {@link auto.$provide $provide}.value, the key being the string name (or token) to associate\n   *        with the value on the injector.\n   */\n  var module = window.module = angular.mock.module = function() {\n    var moduleFns = Array.prototype.slice.call(arguments, 0);\n    return wasInjectorCreated() ? workFn() : workFn;\n    /////////////////////\n    function workFn() {\n      if (currentSpec.$injector) {\n        throw new Error('Injector already created, can not register a module!');\n      } else {\n        var fn, modules = currentSpec.$modules || (currentSpec.$modules = []);\n        angular.forEach(moduleFns, function(module) {\n          if (angular.isObject(module) && !angular.isArray(module)) {\n            fn = ['$provide', function($provide) {\n              angular.forEach(module, function(value, key) {\n                $provide.value(key, value);\n              });\n            }];\n          } else {\n            fn = module;\n          }\n          if (currentSpec.$providerInjector) {\n            currentSpec.$providerInjector.invoke(fn);\n          } else {\n            modules.push(fn);\n          }\n        });","sourceCodeStart":3073,"sourceCodeEnd":3109,"githubUrl":"https://github.com/angular/angular.js/blob/d8f77817eb5c98dec5317bc3756d1ea1812bcfbe/src/ngMock/angular-mocks.js#L3073-L3109","documentation":"angular.mock.module(...) (window.module) only QUEUES module registrations; the queue is consumed when the injector is first created for the current spec. Its workFn runs at injector-creation time, and if currentSpec.$injector is already set — inject() has already run for this spec — no further modules can be registered, so it throws 'Injector already created, can not register a module!'.","triggerScenarios":"Calling module(...) inside an it() after an inject() has run; calling module(...) inside a function passed to inject(); a beforeEach(module(...)) declared after another beforeEach whose function already called inject(); a lazy helper that registers modules on first use, triggered mid-test.","commonSituations":"Mixing module() and inject() in the same spec in the wrong order; trying to register a per-test override module after the injectable fixtures were created; Jasmine beforeAll-style shared setup that injects, followed by per-test module() calls; the module() call is deferred (returned function) and invoked too late.","solutions":["Declare ALL modules before anything injects: put beforeEach(module('a', 'b')) physically before any beforeEach that uses inject().","Inside a single it(), order it module(...) first, then inject(...) — never inject then module.","For per-test variation, register one parameterized module in the outer setup whose config reads a mutable flag/variable the test sets before inject() creates the injector."],"exampleFix":"// before\nvar $httpBackend;\nbeforeEach(inject(function(_$httpBackend_) {\n  $httpBackend = _$httpBackend_; // injector created here\n}));\nbeforeEach(module('mock.responses')); // throws: injector already created\n\n// after\nbeforeEach(module('mock.responses')); // register modules FIRST\nbeforeEach(inject(function(_$httpBackend_) {\n  $httpBackend = _$httpBackend_;\n}));","handlingStrategy":"validation","validationCode":"// Enforce ordering in shared setup: all module() calls before any inject()\nbeforeEach(module('app', 'app.templates', 'test.stubs'));\n// only AFTER the module hooks:\nbeforeEach(inject(function(_$httpBackend_) { /* ... */ }));","typeGuard":null,"tryCatchPattern":"// Fail with actionable guidance when module() is called too late\ntry {\n  module('late.module');\n} catch (e) {\n  if (/Injector already created/.test(e.message)) {\n    throw new Error('Move this module() into a beforeEach that runs before any inject()');\n  }\n  throw e;\n}","preventionTips":["Keep the lifecycle rule: module() (and inject.strictDi) before inject(), always in hook order.","For per-test overrides, register a flag-driven config function early and set the flag per test.","Never call module() inside it() after fixtures were injected."],"tags":["angular-mocks","module","injector","lifecycle","test-setup"],"backgroundTag":"injector-already-created","analyzedSha":"d8f77817eb5c98dec5317bc3756d1ea1812bcfbe","analyzedAt":"2026-08-21T20:36:31.651Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}