import { module } from "./path"; // single module
import Module from "./path"; // default export
import Module, { module } from "./path"; // both
import defaultExport from "module-name";
import * as name from "module-name";
import { export1 } from "module-name";
import { export1 as alias1 } from "module-name";
import { export1 , export2 } from "module-name";
import { export1 , export2 as alias2 , [...] } from "module-name";
import defaultExport, { export1 [ , [...] ] } from "module-name";
import defaultExport, * as name from "module-name";
import "module-name";
var promise = import("module-name");
// app.js
import { add } from './data';
console.log(add(2, 3));
//add this to your package.json
//and make sure you are on node version 13.8.0 or above
"type": "module",
import { module } from "./path"; // then you are good to go to use import
import { name, draw, reportArea, reportPerimeter } from './modules/square.js';
// data.js
export const add = (x, y) => {
return x + y
}
//A function cannot be called unless it was defined in the same file or one loaded before the attempt to call it.
//A function cannot be called unless it is in *the same or greater scope* than the one trying to call it.
//You declare function fn1() in First.js, and then in Second.js you can just call fn1();
//In this case, you need to make sure that First.js and Second.js are in the same folder, or that First's folder is of a higher hierarchy than Second's.
//First.js
function fn1() {
alert();
}
//Second.js
function foo() {
fn1();
}
import contact from './contact.js';
contact('Sara', 25);
// The name is Sara. And age is 25