Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browserify require('restangular') returns empty object

Im facing a problem when I try to use browserify, angularjs and restangular.

When I try to require the npm or bower module, for example require('restangular), browserify returns empty object. This happens when i require any bower or npm modules. When I try to require any local file, everything is working fine.

file structure:

bower_components/
src/
  -client
      -app
          app.js
          backend
                backend.module.js
                -test
                    test.module.ls
                    test.js
node_modules/

app.js

(function() {
    'use strict';
    require('angular');

    var rest = require('restangular');
    console.log(rest);

    module.exports = angular
        .module('app', [
            require('restangular').name,
            require('./test/test.module').name,
        ]);
})(); 

package.json

"browserify": {
    "transform": [
        "browserify-shim"
    ]
},
"browser": {
    "restangular": "./bower_components/restangular/dist/restangular.js"
},
"browserify-shim": {
    "restangular": "restangular"
}

gulpfile.js

gulp.task('browserify', function() {
    gulp.src(['./src/client/app/app.js'])
    .pipe(plugins.browserify({
        insertGlobals: true,
        debug: true
    }))
    .pipe(plugins.concat('bundled.js'))
    .pipe(gulp.dest('./src/client/js'))
});

Can you help me? Thanks in advance.

like image 739
Elnara Avatar asked Jul 31 '15 06:07

Elnara


1 Answers

Just modify your "browser" part in package.json:

"browserify-shim": {
    "angular": {
      "exports": "angular"
    },
 "restangular": {
      "depends": [
        "angular"
      ],
      "exports": "restangular"
    }
}

Hope it will work.

like image 158
Kaushick Gope Avatar answered Sep 30 '22 18:09

Kaushick Gope