Skip to content

Instantly share code, notes, and snippets.

@zaagan
Last active January 8, 2023 20:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaagan/95a00e1ed43878ed1fb6323da0d603d7 to your computer and use it in GitHub Desktop.
Save zaagan/95a00e1ed43878ed1fb6323da0d603d7 to your computer and use it in GitHub Desktop.
Modules Import Export
// Exporting individual features
export let name1, name2, …, nameN;
// also var, const
export let name1 = …, name2 = …, …, nameN;
// also var, const
export function functionName(){...}
export class ClassName {...}
// Export list
export { name1, name2, …, nameN };
// Renaming exports
export { variable1 as name1, variable2 as name2, …, nameN };
// Exporting destructured assignments with renaming
export const { name1, name2: bar } = o;
// Default exports
export default expression;
export default function (…) { … }
// also class, function*
export default function name1(…) { … }
// also class, function*
export { name1 as default, … };
// Aggregating modules
export * from …;
export { name1, name2, …, nameN } from …;
export { import1 as name1, import2 as name2, …, nameN } from …;
export { default } from …;
import defaultExport from "module-name";
import { default as alias } from "module-name";
import * as name from "module-name";
import { export } from "module-name";
import { export as alias } from "module-name";
import { export1 , export2 } from "module-name";
import { foo , bar } from "module-name/path/to/specific/un-exported/file";
import { export1 , export2 as alias2 , [...] } from "module-name";
import defaultExport, { export [ , [...] ] } from "module-name";
import defaultExport, * as name from "module-name";
import "module-name";
var promise = import("module-name");
// Sample : module.js
function cube(x){
return x * x * x;
}
export {cube};
// app.js
import { cube } from 'module.js';
console.log(cube(3)); // 27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment