Vanilla JS Require: Require classes in a Web browser and Node.js

Recommend this page to a friend!
     
  Info   Example   View files Files   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 13 All time: 538 This week: 4Up
Version License JavaScript version Categories
vanilla-require 1.0GNU General Publi...5Node.js, Language, Libraries
Description 

Author

This package can require classes in a way that works in a Web browser and Node.js.

It implements a require functionality making a class load dynamically from a given file when running on Node.js or load via a HTTP request when running from browser.

This way the same class code can be used in a similar way on the Web or on the server side using Node.js.

Picture of Leonardo Mauro Pereira Moraes
  Performance   Level  
Name: Leonardo Mauro Pereira ... <contact>
Classes: 5 packages by
Country: Brazil Brazil
Age: 30
All time rank: 15916 in Brazil Brazil
Week rank: 6 Up1 in Brazil Brazil Up
Innovation award
Innovation award
Nominee: 3x

Winner: 3x

Example

/* Include */ // Instance the vanilla-require class // - arg: directory path const vr = require("../lib.js")(__dirname); // ## Import a Vanilla JS Class console.log("testing: vanilla-require"); // Instance a Vanilla Class // - arg: file path const Calculator = vr.require("Calculator.js"); // Enjoy the Class! let myCalc = new Calculator(), a = 2, b = 3; console.log("-- Vanilla class"); console.log(myCalc); console.log("-- Using the class"); console.log("a:"+a+" b:"+b); console.log("sum:"+myCalc.sum(a, b));

Details

vanilla-require

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.

Installation

npm install --save vanilla-require

Example

See test/test.js for another example.

A Vanilla class (Calculator.js):

class Calculator {

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

Your Node code:

/Include/
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 ~


  Files folder image Files (7)  
File Role Description
Files folder imagetest (2 files)
Plain text file lib.js Class Class source
Accessible without login Plain text file LICENSE Example Example script
Accessible without login Plain text file package-lock.json Data Auxiliary data
Accessible without login Plain text file package.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Class source

  Files folder image Files (7)  /  test  
File Role Description
  Plain text file Calculator.js Class Class source
  Accessible without login Plain text file test.js Example Example script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:13
This week:0
All time:538
This week:4Up