Export and import local modules in Javascript
A lot of examples teach you how to import modules in html. But how do you import own-coded modules in JS file? Here is a simple example showing you how to do. More info refer to JS local modules. Main module test.js imports functions from demo_module.js, which is in the same folder as test.js
var mymodule = require('./demo_module.js');
console.log(mymodule.square(11));
console.log(mymodule.diag(4, 3));In demo_module.js, export your modules
var sqrt = Math.sqrt;
function square(x) {
return x*x;
}
function diag(x, y) {
let v = Math.sqrt(square(x) + square(y));
return v;
};
module.exports.diag = diag;
module.exports.square = square;FEATURED TAGS
AI
Fintech
OpenAI
Generative AI
LLM
Investment
Artificial Intelligence
Startups
AI Agents
AI Safety
AI Governance
Anthropic
Agentic AI
Enterprise AI
Singapore
AI Infrastructure
Open Source AI
AI Research
Quantum Computing
AI Security
Venture Capital
Cybersecurity
Tokenization
China AI
AI Regulation
Stablecoins
Digital Payments
Agentic
Compliance
Payments
Regulation
RWA
AI Policy
US AI
Economy
Banking