0

I've got a module called utilities in a utilities.js file. I am using it for some basic js functions and I want to hide the module from the global namespace. I understand that to do this I should create a module, export it, import it in the needed file, then invoke its functions. However, I cannot seem to properly export the module. I figure this is pretty simple but everything I have tried gives an error. Here is my code:

var utilities = (function(){
return {
    debounce: function(func, wait, immediate){
        var timeout;
        return function() {
            var context = this, args = arguments;
            var later = function() {
                timeout = null;
                if (!immediate) func.apply(context, args);
            };
            var callNow = immediate && !timeout;
            clearTimeout(timeout);
            timeout = setTimeout(later, wait);
            if (callNow) func.apply(context, args);
        };
    }
}
})();

export { utilities };

My error:

application.js:12560 Uncaught SyntaxError: Unexpected token export
2
  • what error do you get? Commented Apr 17, 2019 at 19:57
  • @DanielA.White Just updated my question with the error. Thanks Commented Apr 17, 2019 at 20:01

3 Answers 3

1

Try:

const utilities = function() {
  return {
    debounce: function(func, wait, immediate){
      var timeout;
        return function() {
          var context = this, args = arguments;
          var later = function() {
            timeout = null;
            if (!immediate) func.apply(context, args);
          };
        var callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
       };
     }
   }
 }

export default utilities;

or

function utilities() {
  return {
    debounce: function(func, wait, immediate){
      var timeout;
        return function() {
          var context = this, args = arguments;
          var later = function() {
            timeout = null;
            if (!immediate) func.apply(context, args);
          };
        var callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
       };
     }
   }
 }

 module.exports.utilities= utilities;  
Sign up to request clarification or add additional context in comments.

4 Comments

that exact code gives me application.js:12541 Uncaught ReferenceError: exports is not defined at application.js:12541
Hm, just edited the answer, so give it a try, it works for me, both. @CannonMoyer
Still having the same problem. A quick search led me to CommonJS. Is this a requirement for using the exports keyword? I'm developing this code in a Rails application.
That is a change, look at this blog.arkency.com/2015/04/bring-commonjs-to-your-asset-pipeline ..... than my solution will work
0

it seems that you are exporting on server side. To use export keyword you need to transpile with babel. you can refer to this https://babeljs.io/setup#installation to learn how to set it up

for clientside you can set it up with webpack for example, it also needs babel transpile

npm install --save-dev babel-core
npm install @babel/preset-env --save-dev

Comments

0

this works fine in nodejs, if you are using browser script you need to have <script type="module"> to use export keyword (es modules) or just transpile your code with babel

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.