-3

I have this class:

class Validations {
    static required (value) {
        // code...
    }

    static min (value, arg) {
        // code...      
    }

    static max (value, arg) {
        // code...
    }
}

module.exports = Validations;

In my Node.js back-end i require it like this:

const Validations = require('./Validations.js');

I need to use it in my Vue app as well, but when I use require(), I get error:

Cannot assign to read only property 'exports' of object '#<Object>'

Is there any way to set Babel to transpile it into common JS module?

  • Yes. Yes there is. – Jared Smith Nov 21 '18 at 15:33
  • Possible duplicate of [How to use npm modules in browser? is possible to use them even in local (PC) ? - javascript](https://stackoverflow.com/questions/49562978/how-to-use-npm-modules-in-browser-is-possible-to-use-them-even-in-local-pc) – Jared Smith Nov 21 '18 at 15:38

1 Answers1

0

You can use babel, but I prefer browserify.

Here is how to do it:

npm i browseryfy --save

browserify /full-path-tofile/Validations.js:validations>bundle.js

browserify /full-path-tofile/Validations.js>main.js
  • this is the command to create the module:

Run both of these commands, place the files in the root of your project and require them in the html's head tag as scripts. Remember, after each change, you must rebuild the files again.

To require the module you just do:

const blah = require('validations');
squeekyDave
  • 918
  • 2
  • 16
  • 35