0

What I'm trying to do is define a module of utility functions, browserify it and use it in a polymer component. Since this post: require is not defined error with browserify makes it clear that 'require' is only defined within the scope of the bundle (xx.js), I need help figuring out how to access my exported function.

Here is the gist:

file: x.js

module.exports = function() {console.log("hi");}

I run

browserify x.js> xx.js

file xx.js (edited)

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require==...
  module.exports = function() {console.log("hi");}
},{}]},{},[1]);

My polymer component (edited)

<link rel="import" href="../../bower_components/polymer/polymer.html">
<script type="text/javascript" src="xx.js"></script>

<dom-module id="Hello-app">
  <template> </template>

  <script>
    Polymer({
      is: 'Hello-app',
      ready: function() {/* how do I access the function here, so it prints "hi" */}
    });
  </script>
</dom-module>
Community
  • 1
  • 1
RoyM
  • 1,118
  • 1
  • 9
  • 28

1 Answers1

1

Ah, I should have read the docs for browserify more carefully. I just needed the '-r' option, like so:

  browserify -r ./x.js:hello > xx.js

and now I can modify the ready: line to read

  ready: function() {require('hello')()},
RoyM
  • 1,118
  • 1
  • 9
  • 28