module - Modules
Stability: 2 - Stable
Auto.js has a simple module loading system. In Auto.js, files and modules correspond one-to-one (each file is treated as an independent module).
Example: suppose you have a file named foo.js:
var circle = require('circle.js');
console.log("The area of a circle with radius 4 is %d", circle.area(4));On the first line, foo.js loads the circle.js module in the same directory.
circle.js looks like:
const PI = Math.PI;
var circle = {};
circle.area = function (r) {
return PI * r * r;
};
circle.circumference = (r) => 2 * PI * r;
module.exports = circle;The circle.js module exports two functions: area() and circumference(). By adding properties to the special exports object, you can expose functions and objects from the module.
Local variables inside a module are private. In this example, PI is private to circle.js and does not affect the variable environment of the script that loads it.
The module.exports property can be assigned a new value (e.g. a function or an object).
For example, bar.js uses the square module, and square exports a constructor function:
const square = require('square.js');
const mySquare = square(2);
console.log("The area of the square is %d", mySquare.area());
The `square` module is defined in `square.js`:
// Assigning to `exports` does not change exports; you must use `module.exports`.
module.exports = function(width) {
return {
area: () => width ** 2
};
};