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;
-
Previous
How to setup javascript dvelopment environment in Ubuntu -
Next
Choose front-end program languages
CATALOG
FEATURED TAGS
computer program
javascript
nvm
node.js
Pipenv
Python
美食
AI
artifical intelligence
Machine learning
data science
digital optimiser
user profile
Cooking
cycling
green railway
feature spot
景点
work
technology
F1
中秋节
dog
setting sun
sql
photograph
Alexandra canal
flowers
bee
greenway corridors
programming
C++
passion fruit
sentosa
Marina bay sands
pigeon
squirrel
Pandan reservoir
rain
otter
Christmas
orchard road
PostgreSQL
fintech
sunset
thean hou temple in sungai lembing
海上日出
SQL optimization
pieces of memory
回忆
garden festival
ta-lib
backtrader
chatGPT
stable diffusion webui
draw.io
streamlit
LLM
prompt engineering
fastapi
stock trading
artificial-intelligence