Error: [ng:areq] Argument 'module' is not a function, got Object
Asked Answered
L

1

11

I try to use angular uploader which download it using npm and later browserify it all in single file. The error details can be seen here

I cannot understand the errors because now I'm still new to AngularJS. Here is my code

var angular = require('angular');
angular
    .module('jobApp', [require('angular-material'), require('ng-file-upload')])
    .controller('MainCtrl', ['$rootScope', '$scope', '$mdToast', '$animate', '$http', '$timeout', '$q', '$log', 'Upload',
        function($rootScope, $scope, $mdToast, $animate, $http, $timeout, $q, $log, Upload) {
        'use strict';
        // Initialize the scope variables
        $scope.$watch('files', function () {
            $scope.upload($scope.files);
        });
        $scope.upload = function (files) {
            if (files && files.length) {
                for (var i = 0; i < files.length; i++) {
                    var file = files[i];
                    Upload.upload({
                        url: 'http://sr-recruit.herokuapp.com/resumes',
                        fields: {
                            'name': $scope.name,
                            'email': $scope.email,
                            'about': $scope.about
                        },
                        file: file
                    }).progress(function (evt) {
                        var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                        $scope.log = 'progress: ' + progressPercentage + '% ' +
                                    evt.config.file.name + '\n' + $scope.log;
                    }).success(function (data, status, headers, config) {
                        $scope.log = 'file ' + config.file.name + 'uploaded. Response: ' + JSON.stringify(data) + '\n' + $scope.log;
                        $scope.$apply();
                    });
                }
            }
        };
    }]);

UPDATE

Now am using Browserify-shim to properly enclosed the Angular-file-upload in Commonjs format. Thanks to @ryan Johnson.

Here is the package.json

{
    "browser": {
        "angular": "./node_modules/angular/angular.js",
        "ng-file-upload": "./node_modules/ng-file-upload/dist/ng-file-upload-all.js"
    },
    "browserify-shim": {
        "angular": {
            "exports": "angular"
        },      
        "ng-file-upload": {
            "exports": "angular.module('ngFileUpload').name"
        }
    },
    "browserify": {
        "transform": [
            "browserify-shim"
        ]
    }
}

I still the error

TypeError: Cannot read property '' of undefined

Not sure why. FYI I use Laravel-elixir built in browserify to run the task.

Lymphocytosis answered 12/6, 2015 at 3:22 Comment(6)
The requires array (2nd arg to module) should be an array of stringsMoscow
what do you mean? .module('jobApp', [require('angular-material'), require('ng-file-upload')]) ?Lymphocytosis
It should be .module('jobApp', ['ngMaterial', 'ngFileUpload']). I'm not sure what the node module versions of those export but I bet it's not a string (just checked ng-file-upload and it has no module export)Moscow
if you using browserify. And many suggest to be this wayLymphocytosis
Ok, assuming that actually works, you should only have one array, not two. Ie, you have [require('angular-material')], [require('ng-file-upload')] where it should be [require('angular-material'), require('ng-file-upload')]Moscow
but this one also not workingLymphocytosis
S
19

I noticed that you are using require('ng-file-upload') to include the module. Even though the ng-file-upload module is available through npm the downloaded JS files are not in commonJS format.

In order for the require('ng-file-upload') to work in your example the source file would need to export the module like so:

module.exports = angular.module('ng-file-upload').name;

Since this is not the case for the file you are including you need to shim it to work with Browserify. Here is a good article on Properly shim AngularJS applications using Browserify.

Supplicate answered 12/6, 2015 at 3:44 Comment(4)
FYI, I use laravel-elixir-browserify function. because I thought it will automatically browserify it too. I use npm to manage the angularjs and I dont find it has problem to browserify itLymphocytosis
@MuhaiminAbdul that's because the angular node modules have index.js files with things like module.exports = 'ngMaterial'Moscow
I have try to shim it but no luck because it turn out to be other error TypeError: Cannot read property '' of undefinedLymphocytosis
@MuhaiminAbdul shimming can be tricky with browserify. If we could see your shim setup in your package.json and the browserify config for your task runner (gulp/grunt) that might help.Supplicate

© 2022 - 2024 — McMap. All rights reserved.