Recommend this page to a friend!
  Classes of Leonardo Mauro Pereira Moraes   Vanilla JS Require   Download  
Role: Documentation
Content type: text/markdown
Description: Class source
Class: Vanilla JS Require
Require classes in a Web browser and Node.js
Author: By
Last change: add js classes badges
Date: 2 years ago
Size: 2,039 bytes


Class file image Download


GPLv3 license npm JsClasses GitHub Codacy Badge

The Node.js and npm have an incredible system for the inclusion of packages, made by require. However, for use packages one must export their modules, through module.exports. Yet, it is not possible to use these packages on simple web classes because the web browser does not recognize the module.exports.

This package allows you to develop simple classes to the web and export them to Node.js script as a module. In this sense, you can use your classes in the web browser (pure Javascript) and in the Node.js script, using the vanilla-require package.

Vanilla JS: pure Javascript.


npm install --save vanilla-require


See test/test.js for another example.

A Vanilla class (Calculator.js):

class Calculator {

    sum(num1, num2){
        return num1+num2;

Your Node code:

const vr = require('vanilla-require')(__dirname);
// Instance a Vanilla Class
const Calculator = vr.require('Calculator.js');

// Enjoy the Class!
let myCalc = new Calculator(),
    a = 2, b = 3;

console.log('a:'+a+' b:'+b)
console.log('sum:'+myCalc.sum(a, b));


Also look ~