0

How to import/inject npm module/package bundled with browserify into AngularJS module?

angular.module('name', ['packagename']) 

&

angular.module('name', [require('packagename')])

Both gives error saying module not found. But it is there in bundle.js(bundle created by browserify).

Dalorzo
  • 19,834
  • 7
  • 55
  • 102
Anuj
  • 11
  • 3
  • This is correct.. angular.module('name', ['packagename']) However, I am not sure your bundler is actually adding it to the bundle – Dalorzo May 17 '20 at 05:14

1 Answers1

0

Looks like you went to wrong way.

Browserify is a tool for compiling node-flavored commonjs modules for the browser.

There is no reason to put Browserify inside Angularjs, AngularjS is a client.

It is like to put react native babel or webpack.


About angular.module('name', ['packagename'])

To import any module to your project, check that the module has:

angular.module('packagename', ['ng']). ...

For example https://cdn.rawgit.com/angular-translate/bower-angular-translate/2.5.0/angular-translate.js

has angular.module('pascalprecht.translate', ['ng']).run(/* ... */)

Generally, you can find in DOCs that the module supports Angularjs.

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
  • There are some packages used having 'module.exports' in the project which are usually used in NodeJs. That's why browserify is used. – Anuj May 17 '20 at 13:09
  • There are some packages used having 'module.exports' in the project which are usually used in NodeJs. That's why browserify is used. – Anuj May 17 '20 at 13:09
  • @Anuj, well, NodeJs runs on backend, it is not related to the client that runs statics + JS on client-side. You try to integrate in client something that manipulates with code like webpack – Maxim Shoustin May 17 '20 at 14:23
  • maybe this one will give you some insight: https://stackoverflow.com/questions/7576001/how-to-require-commonjs-modules-in-the-browser – Maxim Shoustin May 17 '20 at 14:25