AngularJS component won't instantiate, "'module' is not a function" - angularjs

I've been asked to do a project in Angular 1, and I've been having a nightmare of a time just trying to get the page to load the app code from bundle.js. I've currently got an error along the lines of
[$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:modulerr] Failed to instantiate module {"_invokeQueue":
[],"_configBlocks":[],"_runBlocks":[],"name":"approveComponent"} due to:
Error: [ng:areq] Argument 'module' is not a function, got Object
but the only approveComponent I've got is essentially a blank placeholder -
import angular from 'angular';
const approve = angular.module('approveComponent', []);
export default approve;
What can I do to get this actually running?
I'm using Webpack 4 to bundle everything. (NO errors from webpack.)

I have seen this error earlier.You need to pass module name not module in the dependency
ngModule = angular.module('webpack', [
require('./webpack/photocrop/client')//this is a object
])
You can simply require the file and just inject by name Like this
var firstModule = require('./webpack/myapp/client')
ngModule = angular.module('webpack', [
'firstModule'//this is should work
])
firstModule(ngModule)
SAMPLE GITHUB REPO

Related

how to import angularjs module which has hyphens in its name in ES6

I am trying to use this md-steppers npm package in my angularjs app with webpack and having a hard time. This is also happening with angular-cache-buster npm package
So, I installed it via npm and used the following import statement
import mdSteppers from 'md-steppers';
and added it in the angularjs DI as below
angular.module('myapp', [ mdSteppers ]
I am getting the error shown below
Uncaught Error: [$injector:modulerr] Failed to instantiate module bolt due to:
Error: [$injector:modulerr] Failed to instantiate module {} due to:
Error: [ng:areq] Argument 'module' is not a function, got Object
http://errors.angularjs.org/1.5.5/ng/areq?p0=module&p1=not%20a%20function%2C%20got%20Object
at eval (webpack:///./node_modules/angular/angular.js?:68:12)
at assertArg (webpack:///./node_modules/angular/angular.js?:1880:11)
at assertArgFn (webpack:///./node_modules/angular/angular.js?:1890:3)
at eval (webpack:///./node_modules/angular/angular.js?:4573:11)
at forEach (webpack:///./node_modules/angular/angular.js?:322:20)
at loadModules (webpack:///./node_modules/angular/angular.js?:4548:5)....
I checked the module name for this npm package and it looks like below
angular.module('md-steppers', [
'material.core',
'material.components.icon']);
/**
* #ngdoc directive
* #name mdStep
* #module md-steppers
*
* #restrict E
Any help is greatly appreciated. thanks
Try the following:
import {name as mdStepper} from 'md-steppers';
angular.module('myapp', [mdSteppers]);
There is no any exported modules in this package.
So, you can use it only this way:
import angular from 'angular';
import mdStepper from 'md-steppers';
angular.module('myapp', ['md-steppers']);

Failed to instantiate module ui-mask angulajs with rquirejs

I need to mask my input in my application. I was using ngMask in angularjs. But i found an issue in ngMask is that if we update the mid value the cursor move to end. I tried to resolve the issue but did not find anything. Even on github this issue has reported but still not resolved. Here is the link
ngMask Issue
Then i decided to use another mask library. I found ui-mask and trying to use in my application but i am getting error when i include in my project. My application is in angularjs and i am using requirejs as well to load the modules.
This is what i have done in main.js:
paths: {
...////
"angular-ui-mask": "../../lib/ui-mask/ui-mask.min",
},
'angular-ui-mask': {
deps: ['angular'],
exports: 'angular-ui-mask'
},
in app.js i have done:
var app = angular.module("app",
[
"ui.router",
"mobile-angular-ui",
"scrollable-table", //for alert triggering
"rzModule", //slider for SEMI diagram
"isteven-multi-select",
"ui.bootstrap",
"cgBusy", "dndLists", 'apm-kpi-widget',
'angular.filter',
"pascalprecht.translate",
"jQueryScrollbar",
"ui.checkbox",
"angular-ui-mask"
])
This is exact i was doing for ngMask. But in this case i am getting error
[$injector:modulerr] http://errors.angularjs.org/1.5.5/$injector/modulerr?
at angular.js:38
at angular.js:4587
at q (angular.js:322)
at g (angular.js:4548)
at bb (angular.js:4470)
at c (angular.js:1746)
at Object.yc [as bootstrap] (angular.js:1767)
at HTMLDocument.<anonymous> (angularAMD.js:449)
at i (jquery-1.12.3.min.js?v=1531811705531:2)
at Object.add [as done] (jquery-1.12.3.min.js?v=1531811705531:2)
What's the issue?

Uncaught Error: [$injector:modulerr] Failed to instantiate module yeomanTestApp due to: Error: [$injector:unpr] Unknown provider: e

I'm using yeoman generator for scaffolding angular web application with requirejs. Its working fine but when I tried to concat and minifying all the js file into a single file through grunt task runner its started giving me above mentioned error. I've researched online about the issue and common solution is I may be mis-spelled any service injecting in the module or service does not exists, I've cross checked again all the spelling, quotation marks etc everything seems fine but still I'm unable to resolve this issue.
Here is my app.js file where my main module with dependencies is listed.
return angular
.module('arteciateYeomanApp', [
'arteciateYeomanApp.controllers.MainCtrl',
'arteciateYeomanApp.controllers.AboutCtrl',
'arteciateYeomanApp.services.Xhr',
'arteciateYeomanApp.services.Common',
'arteciateYeomanApp.controllers.ArtworkCtrl',
'arteciateYeomanApp.controllers.AddAccountCtrl',
'arteciateYeomanApp.controllers.AddArtgroupCtrl',
'arteciateYeomanApp.controllers.AddArtistCtrl',
'arteciateYeomanApp.controllers.AddArtworkCtrl',
'arteciateYeomanApp.controllers.AddCampaignsCtrl',
'arteciateYeomanApp.controllers.AddGenreCtrl',
'arteciateYeomanApp.controllers.AddInstitutionCtrl',
'arteciateYeomanApp.controllers.AdminSignupCtrl',
'arteciateYeomanApp.controllers.ArtistInfoCtrl',
'arteciateYeomanApp.controllers.DirectUserSignupCtrl',
'arteciateYeomanApp.controllers.ErrorCtrl',
'arteciateYeomanApp.controllers.ForgotPasswordCtrl',
'arteciateYeomanApp.controllers.GroupBuyingCtrl',
'arteciateYeomanApp.controllers.LoginCtrl',
'arteciateYeomanApp.controllers.AdminLoginCtrl',
'arteciateYeomanApp.controllers.ResetPasswordCtrl',
'arteciateYeomanApp.controllers.SignupCtrl',
'arteciateYeomanApp.controllers.UnblockUserCtrl',
'arteciateYeomanApp.controllers.UpdatePasswordCtrl',
'arteciateYeomanApp.controllers.DashboardCtrl',
'ngRoute','ngResource']).config(.....);
here is grunt task which I'm running for minifying the js files.
registering task
grunt.registerTask('dev', ['requirejs' ]);
Here is task running script
requirejs : {
compile : {
options : {
baseUrl : "<%= yeoman.app %>/scripts",
mainConfigFile : "<%= yeoman.app %>/scripts/main.js",
name : "main",
out : "requireArterciate.js"
}
}
}
Please let me know if I'm doing something wrong here.
If you need to minify the angularjs code, then use the following standard format syntax to define the controller and to inject the dependencies. Refer Dependency Injection
angular.module('test').controller('testController', testController);
testController.$inject = ['$scope', '$rootScope'];
function testController($scope, $rootScope) {};

angular_1.default.module Uncaught TypeError: Cannot read property 'module' of undefined

I am creating a starter template using angular 1.6, webpack, gulp and typescript. I am able to compile and build my application but app is not bootstrapping. The issue is weird as when I am debugging angular.module is defined and module can be created from console angular.module('testModule', []) but as resume debugger I get exception in core.module.ts at export const CoreModule = angular.module('app.core', []).name;.
I have further investigated this issue by disabling source code and I have found the problem. The above mentioned code compiles to exports.CoreModule = angular_1.default.module('app.core', []).name; where there is no property named default on angular_1.
For code refer https://github.com/muditsaurabh/awesome-ng-starter
Uncaught TypeError: Cannot read property 'module' of undefined
at Object.<anonymous> (app.bundle.js?cd4d443…:1)
at e (vendor.bundle.js?cd4d443…:1)
at Object.<anonymous> (app.bundle.js?cd4d443…:1)
at e (vendor.bundle.js?cd4d443…:1)
at Object.<anonymous> (app.bundle.js?cd4d443…:1)
at e (vendor.bundle.js?cd4d443…:1)
at window.webpackJsonp (vendor.bundle.js?cd4d443…:1)
at app.bundle.js?cd4d443…:1
vendor.bundle.js?cd4d443…:6Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:nomod] Module 'app' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.6.1/$injector/nomod?p0=app
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:377
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:11230
at n (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:10668)
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:11004
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:20796
at r (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:809)
at h (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:20644)
at le (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:22631)
at a (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8560)
at st (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8870)
http://errors.angularjs.org/1.6.1/$injector/modulerr?p0=app&p1=Error%3A%20%…2F%2Flocalhost%3A8000%2Fvendor.bundle.js%3Fcd4d4432605c6f5be56a%3A6%3A8870)
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:377
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:11230
at n (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:10668)
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:11004
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:20796
at r (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:809)
at h (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:20644)
at le (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:22631)
at a (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8560)
at st (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8870)
http://errors.angularjs.org/1.6.1/$injector/modulerr?p0=app&p1=Error%3A%20%…2F%2Flocalhost%3A8000%2Fvendor.bundle.js%3Fcd4d4432605c6f5be56a%3A6%3A8870)
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:377
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:21073
at r (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:809)
at h (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:20644)
at le (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:22631)
at a (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8560)
at st (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8870)
at at (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8109)
at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:11:13916
at HTMLDocument.n (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:17809)
I have replaced import angular from 'angular' with import * as angular from 'angular' and now it is working fine.
Are you sure that AngularJS gets included before your script? The error means that the object angular hasn't been seen by Javascript yet and therefore module cannot be called on it.
You can also just add
"esModuleInterop": true,
to your tsconfig.json instead of using a star import everywhere.

Constant accessible from all modules

I'd like to have constant that is accessible from my other modules.
I tried something like below, but there's exception.
How should I modify my code to make it work?
angular.module('starter', ['starter.Authentication'])
.constant('baseUrl', 'http://myUrl/api/');
angular.module('starter.Authentication', ['baseUrl'])
.factory('AuthenticationService',
['baseUrl',
function (baseUrl) {
var service = {};
return service;
};
Uncaught Error: [$injector:modulerr] Failed to instantiate module starter due to:
Error: [$injector:modulerr] Failed to instantiate module starter.Authentication due to:
Error: [$injector:modulerr] Failed to instantiate module baseUrl due to:
Error: [$injector:nomod] Module 'baseUrl' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
Change:
angular.module('starter.Authentication', ['baseUrl'])
To:
angular.module('starter.Authentication', ['starter'])
This is because starter is a module that holds the baseUrl constant.

Resources